数组、字符串、数学函数

2016-12-12  本文已影响13人  夏意与寒蝉

问答

数组方法里push、pop、shift、unshift、join、split分别是什么作用。(*)

代码题

数组

function push(arr,x){
   arr.splice(arr.length,0,x)
   return arr.length;
}
function pop(arr){  
   return arr.splice(arr.length-1,1)[0];
}
function shift(arr){
  return arr.splice(0,1)[0];
}
function unshift(arr,x){
  arr.splice(0,0,x);
  return arr.length;
}
var prod = {
    name: '女装',
    styles: ['短款', '冬季', '春装']
};
function getTpl(data){
    var a = [];
    a.unshift('<dl class="product">'+'\n');
    a.push('  <dt>'+data.name+'<dt>'+'\n');
    for(var i=0;i<data.styles.length;i++){
        a.push('  <dt>'+data.styles[i]+'<dt>'+'\n')
    }
    a.push('</dl>');
    return a.join('');;
}
var result = getTpl(prod);
console.log(result);
var arr = [ "test", 2, 1.5, false ]
function find(arr,val){
        return arr.indexOf(val);
}
find(arr, "test") // 0
find(arr, 2) // 1
find(arr, 0) // -1
arr = ["a", "b", 1, 3, 5, "b", 2];
function filterNumeric(arr){
   var c =[];
   for(var i=0;i<arr.length;i++){  
       if(typeof arr[i] =='number'){
           c.push(arr[i]);
       }
   }
   return c;
}
newarr = filterNumeric(arr);  //   [1,3,5,2]
arr = ["a", "b", 1, 3, 5, "b", 2];
newarr = filterNumeric(arr); // [1,3,5,2]
function addClass(data,val){
  var data_arr = data.className.split(' ');
  if(data_arr.indexOf(val)==-1){
      data.className+=' '+val;
      return data;
  }
  else{
      return data;
  }
}
function removeClass(data,val){
    var data_arr = data.className.split(' ');
    if(data_arr.indexOf(val) ==-1){
        return ;   
    }
    else{
      var i = data_arr.indexOf(val);
        data_arr.splice(i,1);
        data=data_arr;
        return data ;
    }
}
Paste_Image.png
function camelize(str) { 
 var s = str.split('-');
 if (s.length === 1) { return str;}
 for (var i = 0; i < s.length; i++) {
 s[i] = s[i].charAt(0).toUpperCase() + s[i].substr(1, s[i].length-1);
 } 
 console.log(str = s.join(''));
}
function isPalindrome(str) {
    var s = str.split('');
    var hws=true;
    for (var i = 0; i < s.length / 2; i++) {
        if (s[i] != s[s.length-1-i]) {
            hws=false;
            break;
        }  
    }
    console.log(str,hws? '是回文数':'不是回文数')
    return hws;
}
//方法二
function isPalindrome(data){
    var arr = data.split('');
        if(data == arr.reverse().join('')){
            return "是回文数";
        }else{
            return "不是回文数";
        }
}
Paste_Image.png
function ageSort(arr){
   arr.sort(function(v1, v2){
                return v1.age - v2.age;
            });
   console.log(arr);
}
ageSort(people) // [ bob, mary, john ]
function isNumeric (el){
    return typeof el === 'number'; 
}
arr = ["a",3,4,true, -1, 2, "b"]
function filter(arr,fn){
    var newarr =[]
    for(i=0;i<arr.length;i++){
        if(fn(arr[i])){
           newarr.push(arr[i]);
        }
        
    }
    console.log(newarr);
}
arr = filter(arr,function(val){ return  typeof val === "number" && val > 0 });// arr = [3,4,2]
arr = filter(arr, isNumeric) ; // arr = [3,4,-1, 2]

字符串

function ucFirst(str){
    var s = str.split('');
    s[0]=s[0].toUpperCase();
    console.log(s.join('')) ;
}
//方法二
function ucFirst(str){
            console.log(str = str.replace(str[0], str[0].toUpperCase()));
            //console.log(str[0].toUpperCase() + str.substr(1));
        }
//方法三
function ucFirst(str){
    return str[0].toUpperCase()+str.slice(1,str.length);
}
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,num){
    var s =str.split('');
    if(num>=s.length)
    return str =str.substr(0,num);
    else
    return str =str.substr(0,num)+"..."
}

数学函数

function getn(min,max){
    var i = Math.random();
    return min+Math.floor(i*(max-min));
}
function math4(n){
    var str ="0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
    var r='';
    for(var i=0;i<n;i++){
        var num=Math.floor(s.length);
       r+=s[num];
    }
    return  r;
}
function(len,min,max){
    var arr = new Array(len);
    for(var i=0;i<len;i++){
        arr[i]=min+(Math.random()*(max-min+1));
    }
    retturn arr;
}
function math4(n){
    var str ="0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
    var r='';
    for(var i=0;i<n;i++){
        var num=Math.floor(s.length);
       r+=s[num];
    }
    return  r;
}
上一篇下一篇

猜你喜欢

热点阅读