JavaScript String对象2+Math对象

129 阅读2分钟

String对象2

<script>
        /* let str = "hello kitty"; */
        /* substring(start,end)	提取字符串中两个指定的索引号之间的字符 */
        /* substring方法的两个参数,第一个表示以下标为多少的字符开头,
        包括该字符,第二个表示以下标为多少的字符结尾,不包括该字符 */
        /* substring会返回一个截图后的新的字符串,
        对原来的字符,不会产生影响 */
        /* let n = str.substring(1,3);
        console.log(n,str); */

        /* 如果你只传一个下标,表示你从这个下标开始到最后,
        包括最后一个字符 */
        /* let n = str.substring(1); */
        /* let n = str.substring(1,str.length);
        console.log(n); */

        /* 把首字母变成大写的字符串 用-分割 变成Border-case-good */
        // let arr = ['border','case','good'];
        // /* 把数组分割成字符串 */
        // let str = arr.join('-');
        // console.log(str);
        // let str2 = str.substring(0,1).toUpperCase() + str.substring(1);
        // document.write(str2);

        /* substr(start,number) 也是表示字符串截取 
        第一个参数表示从什么下标开始,并包括第一个下标,
        第二个参数表示从开始的下标开始,截取几个  不推荐使用这种写法*/
        // let str = '你好我的朋友'
        // /* let str2 = str.substring(0,2)
        // console.log(str2); */
        // let str3 = str.substr(0,2)
        // console.log(str3);

        /* slice(start,end) 也是表示字符串的截取, */
        /* slice(start,end)	提取字符串中两个指定的索引号之间的字符 */
        /* slice方法的两个参数,第一个表示以下标为多少的字符开头,
        包括该字符,第二个表示以下标为多少的字符结尾,不包括该字符 */
        /* let str = '我们都是石头人' */
       /*  let str2 = str.slice(0,4);
        console.log(str2); */
        /* slice和substring的区别 */
       /*  let str3 = str.slice(-3)
        console.log(str3); */

        /* 结果是 : 是石 */
        /* let str3 = str.slice(-4,-2)
        console.log(str3); */
        /* 用substring不可以实现 */
       /*  let str3 = str.substring(-4,-2)
        console.log(str3);
 */
        /* substring不能够用负数作为下标,没有负数下标这个功能
        而slice可以,最后一个字符就是-1 依次类推 倒数第二个字符 就是-2 */
        /* let str4 = str.substring(-3)
        console.log(str4); */



        /* ['abc','qwe'] 转成 abcQwe  字符串截取 使用slice 下标使用负数的形式*/
        // let arr = ['abc','qwe'];
        // let str = arr.join('');
        // console.log(  str.slice(-str.length,-3 ) );
        // console.log(  str.slice(-3,-2).toUpperCase() );
        // console.log(  str.slice(-2) );
        // let str2 = str.slice(-str.length,-3) + str.slice(-3,-2).toUpperCase() + str.slice(-2);
        // console.log(str2);
    </script>

Math对象

<script>
        /* 用于执行数学任务 */
        /* ceil()	对数进行上舍入 */
        /* Math.ceil(25.1) =>26 */
        /* Math.ceil(25.9) =>26 */
        /* 负数-号后面的数字越大 值越小 */
        /*  Math.ceil(-25.9) => -25 因为-25比-25.9的值要大*/

        /* floor() 对数进行下舍入 */
        /* 等于帮你把小数点后面的去掉了 */
        /* Math.floor(25.9) =>25 */
        /* Math.floor(25.1) =>25 */
        /* Math.floor(25.16) =>25 */
        /* 负数-号后面的数字越大 值越小 */
        /*  Math.floor(-25.4) => -26 因为-26比-25.4的值要小*/

        /* round() 把数四舍五入为最接近的数 */
        /* Math.round(25.6) => 26 */
        /* Math.round(25.4) => 25 */
        /* Math.round(-25.4) => -25 */
        /* Math.round(-25.6) => -26 */
        /* Math.round(25.5) => 26 */
        /* ★特殊点 满足两个条件 第一个是负数 第二个小数位是5  */
        /* Math.round(-25.5) => -25 */
        /* document.write( Math.round(-25.5) );  */

        /* random() 返回0.0~1.0之间的随机数 */
        /* 包括0,但是不包括1 */
        /*  document.write( Math.random() ); */

        /* 随机出现 1-10 之间的数 包括1 不包括10 */
        /* 公式 Math.floor( Math.random()*(max-min) ) + min */
        /* document.write( Math.floor(Math.random()*(10-1)) + 1 ); */

        /* 随机出现 2-10 之间的数 包括2 也包括10 */
        /* 公式 Math.floor( Math.random()*(max-min+1) ) + min */
        /* document.write( Math.floor(Math.random()*(10-2+1)) + 2 ); */


        /* 
            使用Math对象随机产生10到100的十个数字(包括10 也包括100),
            并对这十个随机数排重
        */
       let arr1 = []; /* 用来存随机数 */
       let arr2 = []; /* 用来存放去重后的数据 */
       for(var i=0;i<10;i++){
        /*  包括10 也包括100 */
        //    arr1.push( Math.floor( Math.random()*(100-10+1) )+10 );
        /* 不包括10 也不包括100 取10-100之间的 */
        /* arr1.push( Math.floor( Math.random()*(99-11+1) ) + 11 ); */
        /* 1-10 不包括 1 和10 */
        arr1.push( Math.floor( Math.random()*(9-2+1) ) + 2 );
       }
       for(var j=0;j<10;j++){
           if(arr2.indexOf(arr1[j])==-1){
               arr2.push(arr1[j])
           }
       }
      /*  arr2.sort(function(a,b){
           return a-b;
       }) */
       /* 改成冒泡排序 */
       for(var a in arr2){
           for(var b in arr2){
               /* 后一个数比前一个数大的情况下 */
               if(arr2[a]<arr2[b]){
                   /* 先存下小的值 */
                  var temp = arr2[a];
                  arr2[a] = arr2[b];
                  arr2[b] = temp;
               }
           }
       }
       
       /* 10到100的十个数字(不包括10 也不包括100 取10-100之间的) */
       console.log(arr2);
       
        /* 
            使用Math对象随机产生10到100的十个数字,
            (不包括100)并对这十个随机数排序
        */
        // let arr = [];
        // for(var i=0;i<10;i++){
        //     let num = Math.floor( Math.random()*(100-10) ) + 10;
        //     arr.push(num)
        // }
        // arr.sort(function(a,b){
        //     return a - b;
        // })
        // console.log(arr);