博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
js练习题
阅读量:7151 次
发布时间:2019-06-29

本文共 7057 字,大约阅读时间需要 23 分钟。

/*        1,编写一个javscript函数 fn,        该函数有一个参数 n(数字类型),其返回值是一个数组,        该数组内是 n 个随机且不重复的整数,且整数取值范围是 [2, 32]。        */        function fn(n){            var max = 32,min = 2;   // 这边设置动态的数,以便后续可调节            //先做n的校验            if (isNaN(n)) return;   // 不能非数字            if (n<1||n>31) return;  // 不能小于1或者大于31            if (n%1 !== 0) return;  // 不能非整数                        var c = max - min + 1;  // 共有这么多数字            var arr = [];   // 要返回的初始空数组            for (var i = 0; i< n; i++) {                if (i==0) {                 // 如果是第一个数字,那么不用检验,肯定是不存在的                    var a = Math.floor(Math.random()*c+min);                    // [min, max]区间内的随机数,用Math.floor(Math.random()*(max-min+1)+min);                    arr.push(a);                } else {                    checkExistNum(arr,c,min);                };                            };            return arr        };        function checkExistNum(arr,c,min){            var a = Math.floor(Math.random()*c+min);            var isRepeat = arr.indexOf(a) === -1;            if (isRepeat) {                arr.push(a);                return            } else {                checkExistNum(arr,c,min);            }        };        /*2,编写尽可能简洁的javascript代码,找到在第一个数组array1中出现,而在第二个数组array2中没有出现的数字。*/        function fn2(array1, array2){            var array3 = array1.filter(function(item){                return (array2.indexOf(item) === -1)            });            return array3        }                /*3,编写函数,用于过滤一个数组内重复的元素,并用这些元素重构一个新的数组,新数组内也不能有重复元素。*/                function fn3(array) {            var array2 = filterRepeat(array);   // 过滤后的数组,无重复            var array3 = [];            array.forEach(function(item,index){                if (array.indexOf(item) !== index) {                    array3.push(item)                }            });            var array4 = filterRepeat(array3);            var obj = {                '过滤后的数组': array2,                '过滤后的元素组成的数组': array4            };            return obj        };        function filterRepeat(array){            var array2 = array.filter(function(item,index){                return (array.indexOf(item) === index)            });            return array2        }        /*4.现有一个数组(元素为数字,并且有可能重复),请给Array.prototype增加一个方法(方法名自取),该方法能去掉数组中全部最大和最小的数字。*/        // 我想到有一个正排序数组的方法,然后直接头部和尾部就是最大和最小值        function compareNum(a,b){            if (a
b) { return 1 } else { return 0 } } Array.prototype.extremeNumber = function(){ var that = this.slice(); // 这里有个注意点,不能直接写var that = this;因为this是数组,是object不是基本类型,复制的话是按引用类型传递,如果修改that那么this也会改变,所以要用slice,这里也可以用concat,但是如果当前环境是webkit,就用concat更快,如果是普通浏览器,就用slice更快 var newArray = that.sort(compareNum); var a = newArray.length; var minNum = newArray[0]; // 一定是最小值 var maxNum = newArray[a-1]; // 一定是最大值 var array = this.filter(function(item){ return (item !== minNum && item !== maxNum) }); return array }; // 还有一个是用Math.max和Math.min的方法依次对比,配合归并,事实证明,这段代码最少最优 Array.prototype.extremeNumber = function(){ var maxNum = this.reduce(function(prev, cur){ return Math.max(prev, cur) }); // 这里也是求一个数组最大值的方法 var minNum = this.reduce(function(prev, cur){ return Math.min(prev, cur) }); // 这里也是求一个数组最小值的方法 var array = this.filter(function(item){ return (item !== minNum && item !== maxNum) }); return array } /*5, 如何准确判断一个javascript对象是数组? */ function judgeArray(array){ if (typeof array === 'object' && array instanceof Array === true) { return true } else { return false } } // 还有一种是通用的方法,Object.prototype.toString,这个方法还可以判断其他类型 function judgeArray(array){ return Object.prototype.toString.call(array)=='[object Array]'; } /*6,写出一个javascript的函数,实现对一个数组去重的功能。*/ // 这个跟上面那个3一样了,再写一遍 function removeRepeat(array){ var repeatArray = array.filter(function(item,index){ return (array.indexOf(item) == index) }); return repeatArray } /*7,请写出一个函数,功能是删除数组的指定下标元素。*/ function deleteElement(index,array){ if (index>array.length-1) return; var newArr = array.filter(function(item,a){ return (a !== index) }); return newArr } // 我忘记了有个方法叫splice function deleteElement(index,array){ array.splice(index, 1); return array } /*8,请写一个函数removeVoid(arr),删除该数组中值为“null,undefined”的项,返回原数组。*/ function removeVoid(arr){ var newArr = arr.filter(function(item){ return (item !== null && item !== undefined) }); return newArr } /*9,请给Array本地对象增加一个原型方法,它的用途是删除数组中重复的条目,并将数组返回。*/ Array.prototype.removeRepeat = function(array){ var repeatArray = array.filter(function(item,index){ return (array.indexOf(item) == index) }); return repeatArray } /*10, 用js实现随机选取10~100之间的10个数字,存入一个数组,并排序。*/ function sortNumber(){ var newArr = []; var max = 100, min = 10; var c = max - min + 1; for(var i = 0; i< 10; i++) { var randomNum = Math.floor(Math.random()*c + min); // 10-100区间的随机数字 newArr.push(randomNum); }; newArr.sort(compareNum); return newArr } function compareNum(a,b){ if (a
b) { return 1 } else { return 0 } } /*11, 把两个数组合并,并删除第二个元素。*/ function concatArr(arr1, arr2) { var newArr = arr1.concat(arr2); newArr.splice(2,1); return newArr } /*12,根据每个元素的i属性,由小到大排序如下数组。*/ function sortArr(array){ var newIArr = [], newArr=[]; array.forEach(function(item){ // 这边我就不判断是否存在i属性了,因为题中给了具体的数组,确定每个元素都有i属性 newIArr.push(item['i']) }); newIArr.sort(compareNum); newIArr.forEach(function(it){ array.forEach(function(item){ if(item['i'] == it) { newArr.push(item) } }) }); return newArr } function compareNum(a,b){ if (a
b) { return 1 } else { return 0 } } // 做复杂了,最简单的如下 function sortArr(arr){ var newArr = arr.sort(function(a,b){ return a - b }); return newArr }

 

转载于:https://www.cnblogs.com/yanchenyu/p/7568720.html

你可能感兴趣的文章
本地计算机 上的 MySQL 服务启动后停止。某些服务在未由其他服务或程序使用时将自动停止。...
查看>>
调用系统拍照
查看>>
Java——IO之常量及路径
查看>>
NSUserDefaults保存应用中的数据
查看>>
用Gvim建立IDE编程环境 (Windows篇)_Nothing is impossible for a willing heart._百度空间...
查看>>
poj 1386 Play on Words
查看>>
到了最后出现败笔
查看>>
Chrome 插件
查看>>
《Effective C#》读书笔记——条目24:用委托实现回调<使用C#表达设计>
查看>>
c++的重载,覆盖与隐藏
查看>>
How to make a combo box with fulltext search autocomplete support?
查看>>
大数据的三个入口
查看>>
void指针
查看>>
基本类型赋值转换规则表
查看>>
hackerrank-knapsack
查看>>
取消word中所有超链接
查看>>
AABB边框、OBB边框、通过比较球包围
查看>>
Atitit. 软件开发中的管理哲学--一个伟大的事业必然是过程导向为主 过程导向 vs 结果导向...
查看>>
JS产生随机数的几个用法!
查看>>
浏览器默认样式(User Agent Stylesheet)
查看>>