美文网首页
一些简单的函数

一些简单的函数

作者: 来去匆匆不过过客 | 来源:发表于2017-07-27 00:08 被阅读0次

    1.数字转换为字母

    var ch = String.fromCodePoint( parseInt(t) + 64)  //大写
    ch = String.fromCodePoint( parseInt(t) + 96 )      //小写
    

    2.== & ===
    https://www.zhihu.com/question/20348948
    2.数学运算

    parseInt(4/3)            //取整
    Math.ceil(4/3)           //向上取整
    Math.round(4/3)       //四舍五入
    Math.floor(4/3)         //向下取整
    

    Math函数的一些其他方法

    abs(x)            //绝对值
    exp(x)            //e的指数
    max(x,y)
    min(x,y)
    pow(x,y)        //x的y次幂
    random()        //0-1之间的随机数
    round(x)        //四舍五入为一个最接近的整数
    valueOf()       //返回Math对象的原始值
    

    3.输出

    • 控制台输出console.log()
    • 对话框输入:a = prompt(1+2=?)?

    对话框输入的结果即为a的值

    4.增强for循环

    • for in(i为数组下标
    
    var stu_scores = {'杨璐':131,
        '王雪':131,
        '韩林霖':127,
        '沙龙逸':123,
        '李鉴学':126,
        '韩雨萌':129,
        '刘帅':116,
        '康惠雯':114,
        '刘钰婷':115};
    var stu_names = ['杨璐',
        '王雪',
        '韩林霖',
        '沙龙逸',
        '李鉴学',
        '韩雨萌',
        '刘帅',
        '康惠雯',
        '刘钰婷'];
    var scores = [];
    var highest_score = 0;
    //使用for循环取出成绩数组,打印所有成绩,找到做高分
    for(var i in stu_names){
        var score = stu_scores[stu_names[i]]
        scores.push(score)
        if(score > highest_score)
            highest_score = score
    }
    //获取所有学生的分数(只包含学生分数不包含学生姓名)存到scores中
    
    var highest_score = scores[0];
    //使用for循环找出学生成绩的最高分
    
    console.log('学生成绩的最高分:'+highest_score);
    
    
    • forEach
    stu_names.forEach(name =>{
     var score = stu_scores[name]
        scores.push(score)
        if(score > highest_score)
            highest_score = score
    })
    
    • forEach
    stu_names.forEach(function(name){
     var score = stu_scores[name]
        scores.push(score)
        if(score > highest_score)
            highest_score = score
    })
    

    相关文章

      网友评论

          本文标题:一些简单的函数

          本文链接:https://www.haomeiwen.com/subject/kbeakxtx.html