[if !supportLists]1. [endif]写出10个常用的数组方法
<script> // indexOf() lastIndexOf() includes() // unshift() shift() push() pop() // concat() slice() reverse() // sort() join() splice() // forEach() map() filter() some() every() // find() findIndex() reduce() script>
[if !supportLists]2. [endif]写出10个常用的字符串方法
<script> // indexOf() lastIndexOf() repeat() trim() // charAt() charCodeAt() endsWith() startsWith() // concat() toUppercase() toLowercase() // search() match() split() replace() // substring() slice() includes()script>
[if !supportLists]3. [endif]存一个有效期为7天的cookie,key=nickname,val=Ace
<script> var now = new Date(); now.setDate(now.getDate() + 7) document.cookie = "nickname=Ace;expires=" + now; console.log(document.cookie);script>
4.删除上面那个cookie
<script> var now = new Date(); now.setDate(now.getDate() - 1) document.cookie = "nickname=Ace;expires=" + now;script>
5.获取地址栏 age=20&pirce=30&keywords=lv&cc=1中的keywords参数的值
<script> // 获取地址栏 age=20&pirce=30&keywords=lv&cc=1 中的keywords参数的值 var str = 'age=20&pirce=30&keywords=lv&cc=1' function getValue(key) { var arr1 = str.split("&"); console.log(arr1); for (let i = 0; i < arr1.length; i++) { console.log(arr1[i]); var arr2 = arr1[i].split("="); console.log(arr2); if (arr2[0] == key) { return arr2[1] } } } console.log(getValue('keywords'));script>
[if !supportLists]6. [endif]获取30~40之间的一个随机数
<script> // Math.floor(Math.random() * (max-min+1) + min) var randomNum = Math.floor(Math.random() * 11 + 30) console.log(randomNum);script>
[if !supportLists]7. [endif]如何获取当前是星期几,汉字
<script> var now = new Date(); var week = ['星期日','星期一','星期二','星期三','星期四','星期五','星期六'] console.log(week[now.getDay()]);script>
[if !supportLists]8. [endif]递归方式获取10以内数字的和
<script> function fn(num) { if (num <= 1) { return 1; } else { return num + fn(num - 1) } } console.log(fn(10));script>
[if !supportLists]9. [endif]写一个补零函数,如repairZero(8)变成“08”
<script> // 9.写一个补零函数,如 repairZero(8) 变成 “08” function repairZero(num) { if (num < 10) { return "0" + num } else { return "" + num } } console.log(repairZero(8));script>
或者:
<script> // 9.写一个补零函数,如 repairZero(8) 变成 “08” function repairZero(num) { return num = num < 10 ? '0' + num : "" + num } console.log(repairZero(8));script>
[if !supportLists]10. [endif]手写节流函数
<script> function debounce(fn, wait) { let timer; return function () { console.log(timer); if (timer) { return; } timer = setTimeout(() => { fn.apply(this, arguments); // 正常 timer = null; }, wait); } }script>
[if !supportLists]11. [endif]手写防抖函数
<script> function debounce(fn, delay) { let timeout; return function () { clearTimeout(timeout); timeout = setTimeout(() => { // fn(); // 报错 fn.apply(this, arguments); // 正常 }, delay); } }script>
[if !supportLists]12. [endif]写一个函数,将{a:1,b:2}转换成a=1&b=2的字符串
<script> // 将{a:1,b:2}转换成a=1&b=2的字符串 var arr = []; var obj = {a:1,b:2}; for (var key in obj) { arr.push(key + '=' + obj[key]); } console.log(arr.join('&'));script>
[if !supportLists]13. [endif]写一个函数,得到字符串aabcdcacd中每个字符串的出现次数
方法一:
<script> // 得到字符串 aabcdcacd中每个字符串的出现次数 var str = 'aabcdcacd' function charStat(str){ var obj = {}; for (let i = 0; i < str.length; i++) { if (obj[str[i]]) { obj[str[i]]++ } else { obj[str[i]] = 1; } } return obj } console.log(charStat(str));script>
方法二:
<script> // 得到字符串 aabcdcacd中每个字符串的出现次数 var str = 'aabcdcacd' var countedStr = str.split("").reduce(function (allStr, str) { console.log(allStr,str); if (str in allStr) { allStr[str]++; } else { allStr[str] = 1; } return allStr; }, {}); console.log(countedStr);script>
方法三:
<script> // 得到字符串 aabcdcacd中每个字符串的出现次数 var str = 'aabcdcacd' var res = [...str].reduce( (a,b) => { a[b] ? a[b]++ : a[b]=1; return a; }, {}); console.log(res);script>
14.写一个函数,将数组中[11,22,11,32,22,4]去除重复
<script> // 将数组中[11,22,11,32,22,4]去除重复 var arr = [11,22,11,32,22,4]; var arr2 = []; // 也可以使用indexOf()方法 arr.forEach(item => { if (!arr2.includes(item)) { arr2.push(item) } }); console.log(arr2); // 使用Set对象 var m = new Set(arr); console.log(m); console.log([...m]);script>
15.写一个函数,实现数组的join功能,如 _.join([1,2,3],'&')得到 1&2&3
<script> // 如 _.join([1,2,3],'&')得到 1&2&3 Array.prototype.myjoin = function(str){ var res = "" var str = str || ',' for (let i = 0; i < this.length-1; i++) { res += this[i] + str; } return res + this[this.length-1]; } console.log([1,2,3].myjoin("&"));script>