數(shù)組

問(wèn)答

1.數(shù)組方法里push、pop、shift、unshift、join、split分別是什么作用.(*)

  • 數(shù)組的末尾添加新的元素 - push()
  • 刪除數(shù)組的最后一個(gè)元素 - pop()
  • 刪除數(shù)組的第一個(gè)元素 - shift()
  • 在數(shù)組的開(kāi)頭添加新元素 - unshift()
  • 用數(shù)組的元素組成字符串 - join()
  • 把字符串分解成一系列子串保存在數(shù)組里,返回?cái)?shù)組 - split()
var str="abcdef";
var n=str.split("");
console.log(n);//["a", "b", "c", "d", "e", "f"]

代碼題

數(shù)組

1.用 splice 實(shí)現(xiàn) push、pop、shift、unshift方法 (***)
var arr=[1,2,3,4,5];
arr.splice(arr.length,0,"content");//push
arr.splice(arr.length-1,1);//pop
arr.splice(0,1);//shift
arr.splice(0,0,"content");//unshift
2.使用數(shù)組拼接出如下字符串 (***)
var prod = {
    name: '女裝',
    styles: ['短款', '冬季', '春裝']
};
function getTpl(data){
//todo...
};
var result = getTplStr(prod);  //result為下面的字符串
<dl class="product">
    <dt>女裝</dt>
    <dd>短款</dd>
    <dd>冬季</dd>
    <dd>春裝</dd>
</dl>
var prod = {
        name: '女裝',
        styles: ['短款', '冬季', '春裝']
    };

    function getTpl(data) {
        var arr = [];
        arr.push('<dl class="product">');
        arr.push("<dt>"+prod.name+"</dt>");
        for (var i = 0; i < prod.styles.length; i++) {
            arr.push("<dd>"+prod.styles[i]+"</dd>")
        }
        arr.push('</dl>');
        arr1=arr.join("");
        return arr1;
    };
    var result = getTpl(prod);
    console.log(result);
3.寫(xiě)一個(gè)find函數(shù),實(shí)現(xiàn)下面的功能 (***)
var arr = [ "test", 2, 1.5, false ]
find(arr, "test") // 0
find(arr, 2) // 1
find(arr, 0) // -1
var arr = ["test", 2, 1.5, false];

    function find(argument, inner) {
        for (var i = 0; i < argument.length; i++) {
            if (inner == argument[i]) {
                return i;
            } 
           
        }
        return -1;

    }
    console.log(find(arr, false));
4.寫(xiě)一個(gè)函數(shù)filterNumeric,實(shí)現(xiàn)如下功能 (****)
arr = ["a", 1, "b", 2];
newarr = filterNumeric(arr);  //   [1,2]
 arr = ["a", 1, "b", 2, 3, 7, "12"];
    newarr = filterNumeric(arr); //   [1,2]
    function filterNumeric(argument) {
        newarr = [];
        for (var i = 0; i < arr.length; i++) {
            if (typeof arr[i] == "number") {
                newarr.push(arr[i]);
            }
        }
        console.log(newarr);
    }
5.對(duì)象obj有個(gè)className屬性,里面的值為的是空格分割的字符串(和html元素的class特性類(lèi)似),寫(xiě)addClass、removeClass函數(shù),有如下功能:(****)
var obj = {
  className: 'open menu'
}
addClass(obj, 'new') // obj.className='open menu new'
addClass(obj, 'open')  // 因?yàn)閛pen已經(jīng)存在,此操作無(wú)任何辦法
addClass(obj, 'me') // obj.className='open menu new me'
console.log(obj.className)  // "open menu new me"

removeClass(obj, 'open') // obj.className='menu new me'
removeClass(obj, 'blabla')  // 不變
var obj = {
        className: 'open menu'
    }

    function addClass(argument, value) {

        var arr = argument.className.split(' ');
        if (arr.indexOf(value) == -1) {
            arr.push(value);
        }

        argument.className = arr.join(' ');
        console.log(argument.className);
    }

    function removeClass(argument, value) {
        var arr = argument.className.split(' ');
        if (arr.indexOf(value) != -1) {
            arr.splice(arr.indexOf(value), 1);
        }
        
        argument.className = arr.join(' ');
        console.log(argument.className);
    }

    addClass(obj, 'new') // obj.className='open menu new'
    addClass(obj, 'open') // 因?yàn)閛pen已經(jīng)存在,此操作無(wú)任何辦法
    addClass(obj, 'me') // obj.className='open menu new me'
    console.log(obj.className) // "open menu new me"

    removeClass(obj, 'open') // obj.className='menu new me'
    removeClass(obj, 'blabla') // 不變
6.寫(xiě)一個(gè)camelize函數(shù),把my-short-string形式的字符串轉(zhuǎn)化成myShortString形式的字符串,如 (***)
camelize("background-color") == 'backgroundColor'
camelize("list-style-image") == 'listStyleImage'
function camelize(str) {
        var x = str.split('-');
        var sum = x[0];
        for (var i = 1; i < x.length; i++) {
            sum += x[i].slice(0, 1).toUpperCase() + x[i].slice(1);
        }
        console.log(sum);
    }
    camelize("background-color");
    camelize("list-style-image");
7.如下代碼輸出什么?為什么? (***)
var console.log('hello hunger valley')=1
arr = ["a", "b"];
arr.push( function() { alert(console.log('hello hunger valley')) } );
arr[arr.length-1]()  // ?

瀏覽器窗口彈出undefined,控制臺(tái)輸出hello hunger valley;
arrarr.length-1執(zhí)行alert,console.log('hello hunger valley')被認(rèn)定為變量,但是沒(méi)有定義.

8.寫(xiě)一個(gè)函數(shù)filterNumericInPlace,過(guò)濾數(shù)組中的數(shù)字,刪除非數(shù)字 (****)
arr = ["a", 1, "b", 2];
//對(duì)原數(shù)組進(jìn)行操作,不需要返回值
filterNumericInPlace(arr);
console.log(arr)  // [1,2]
arr = ["a", 1, "b", 2, 3, 7, "12"];
    function filterNumericInPlace(argument) {
        for (var i = 0; i < argument.length; i++) {
            if (typeof argument[i] != "number") {
              argument.splice(i,1);
            }
        }
        console.log(argument);
    }
    filterNumericInPlace(arr);
9.寫(xiě)一個(gè)ageSort函數(shù)實(shí)現(xiàn)如下功能 (***)
var john = { name: "John Smith", age: 23 }
var mary = { name: "Mary Key", age: 18 }
var bob = { name: "Bob-small", age: 6 }
var people = [ john, mary, bob ]
ageSort(people) // [ bob, mary, john ]

我不會(huì)

10.寫(xiě)一個(gè)filter(arr, func) 函數(shù)用于過(guò)濾數(shù)組,接受兩個(gè)參數(shù),第一個(gè)是要處理的數(shù)組,第二個(gè)參數(shù)是回調(diào)函數(shù)(回調(diào)函數(shù)遍歷接受每一個(gè)數(shù)組元素,當(dāng)函數(shù)返回true時(shí)保留該元素,否則刪除該元素)。實(shí)現(xiàn)如下功能: (****)
function isNumeric (el){
    return typeof el === 'number'; 
}
arr = ["a", -1, 2, "b"]

arr = filter(arr, isNumeric) ; // arr = [-1, 2],  過(guò)濾出數(shù)字
arr = filter(arr, function(val) { return val > 0 });  // arr = [2] 過(guò)濾出大于0的整數(shù)
function isNumeric(el) {
        return typeof el === 'number';
    }
    function filter(arr,callback){
        var arr1=[];
        for (var i = 0; i < arr.length; i++) {
            if (callback(arr[i])) {
                arr1.push(arr[i]);
            }
            
        }
        return arr1;
    }
    arr = ["a", -1, 2, "b",-45,12,-9,4,2,'qwr'];
    arr = filter(arr, isNumeric);//過(guò)濾出數(shù)字
    console.log(arr);
    arr = filter(arr, function(val) { return val > 0 });//// arr = [2] 過(guò)濾出大于0的整數(shù)
    console.log(arr);

字符串

1.寫(xiě)一個(gè) ucFirst函數(shù),返回第一個(gè)字母為大寫(xiě)的字符 (***)
ucFirst("hunger") == "Hunger"
ucFirst("hunger") ;
    function ucFirst(argument) {
       arr=argument.split("");
       arr=arr[0].toUpperCase()+arr.slice(1).join("");
       console.log(arr);
    }
2.寫(xiě)一個(gè)函數(shù)truncate(str, maxlength), 如果str的長(zhǎng)度大于maxlength,會(huì)把str截?cái)嗟絤axlength長(zhǎng),并加上...,如 (****)
truncate("hello, this is hunger valley,", 10)) == "hello, thi...";
truncate("hello world", 20)) == "hello world"
truncate("hello, this is hunger valley,", 10)) == "hello, thi...";
    truncate("hello world", 20)) == "hello world"

    function truncate(str,maxlength) {
        if (str.length>maxlength) {
            str=str.slice(0,maxlength)+"...";
        }
        console.log(str);
    }

數(shù)學(xué)函數(shù)

1.寫(xiě)一個(gè)函數(shù)limit2,保留數(shù)字小數(shù)點(diǎn)后兩位,四舍五入, 如: (**)

var num1 = 3.456
limit2( num1 );  //3.46
limit2( 2.42 );    //2.42
var num1 = 3.456 
    limit2( num1 ); //3.46 
    limit2( 2.42 ); //2.42 
    function limit2(num) { 
        num=num*100; 
        num=Math.round(num); 
        num=num/100; console.log(num); 
    }

2.寫(xiě)一個(gè)函數(shù),獲取從min到max之間的隨機(jī)數(shù),包括min不包括max (***)

function getRandomArbitrary(min, max) {
  return Math.random() * (max - min) + min;
}

3.寫(xiě)一個(gè)函數(shù),獲取從min都max之間的隨機(jī)整數(shù),包括min包括max (***)

function getRandomInt(min, max) {
  return Math.floor(Math.random() * (max - min + 1)) + min;
}

4.寫(xiě)一個(gè)函數(shù),獲取一個(gè)隨機(jī)數(shù)組,數(shù)組中元素為長(zhǎng)度為len,最小值為min,最大值為max(包括)的隨機(jī)數(shù) (***)

function getRandomArr(min, max, len) {
        var arr = [];
        for (var i = 0; i < len; i++) {
            arr.push(Math.random() * (max - min) + min);
        }
        return arr;
    }
    arr1 = getRandomArr(10, 20, 10);
    console.log(arr1);
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

推薦閱讀更多精彩內(nèi)容