美文网首页前端(Front-end)
JavaScript--FreeCodeCamp

JavaScript--FreeCodeCamp

作者: 暗黑破坏球嘿哈 | 来源:发表于2016-11-08 08:43 被阅读39次
    1. "=='
      string中如果需要用嵌套引号,可以转义双引号也可以交替使用单引号

    想到一个段子(不无聊的可略过)
    问:有没有什么无意中暴露自己的经历?
    答:某日,一妹子问string这单词什么意思,张口就答字符串。

    1. 如果想通过数组索引来改变一个字符串某位置的值是不行的,需要重新给该字符串赋值,应该是值传递和引用传递的区别
    //错误示范
    var myStr = "Bob";
    myStr[0] = "J";
    //正确示范
    var myStr = "Bob";
    myStr[0] = "J";
    

    (太基础了,看的我好无聊,感觉马上可以写一个关于各种教程网站的测评了。。。)

    1. arr.push(element-value); 在数组后面拼接element
      arr.pop();
    2. .shift(); function to remove the first item from myArray
      unshift(element-value); adds the element at the beginning of the array.
    3. It is possible to have both local and global variables with the same name. When you do this, the local variable takes precedence over the global variable.
    4. 注意:把函数返回值赋给变量和把函数赋给变量不一样
    5. a queue is an abstract Data Structure where items are kept in order。New items can be added at the back of the queue and old items are taken off from the front of the queue
    6. 也讲到了调用object属性时候的两种方式:一种是.语法,一种是[],区别在于[]中可以放有空格的字符串,比如obj["hi xy"], 当然这个“hi xy”也可以赋值给一个变量hi,然后obj[hi], 一个意思
    7. 接下来,能表示某个属性,也就可以改变属性值或者增加属性并赋给属性一个值,注意,删除delete obj.prop;
    8. 从switch case表示方法换成object中k-v表示
    9. check if the property of a given object exists or not. .hasOwnProperty(propname) returns true or false.
    10. record collection answer-gist link
      对英文的需求有点费解,但程序很简单
    11. 遍历obj记得用for in
    function lookUpProfile(firstName, prop){
      for(var i = 0; i <contacts.length; i++){
            if (firstName===contacts[i].firstName){
               for(var p in contacts[i]){
                 if (prop===p){
                   return contacts[i][p];
                 }
               }
                return "No such property";
            }  
      }
      return "No such contact";
    }
    
    1. generate a random whole number between 0 and 9
      要whole number用Math.floor(),向下取整,down to the closest number.
      Math.floor((Math.random()*10))
      random(): Return a random number between 0 (inclusive) and 1 (exclusive):
    2. 在min到max中选一个随机整数,注意上面写的,左闭右开,所以要+1
      return Math.floor(Math.random() * (myMax - myMin + 1)) + myMin;
    3. 正则/\d+/gi匹配数字,g--global \s--space
    // This code counts the matches of expression in testString
    var andCount = testString.match(expression).length;
    
    1. To create objects using constructor functions.
      A constructor function is given a capitalized name to make it clear that it is a constructor
      this refers to the new object being created by the constructor
    2. private prop
    var Car = function() {
        // this is a private variable
          var speed = 10;
          // these are public methods
          this.accelerate = function(change) {
              speed += change;
          };
          this.decelerate = function() {
              speed -= 5;
          };
          this.getSpeed = function() {
              return speed;
          };
      };
    var Bike = function() {
          var gear;
          this.getGear = function(){
            return this.gear;
          };
          this.setGear = function(Gear){
            this.gear = Gear;
          };
        // Only change code below this line.
    };
    
    1. map
    var oldArray = [1, 2, 3];
    var timesFour = oldArray.map(function(val){  
        return val * 4;
    });
    console.log(timesFour); // returns [4, 8, 12]
    
    1. reduce, iterate through an array and condense it into one value.
      比如求所有元素和
    var array = [4,5,6,7,8];
    var singleVal = 0;
     singleVal = array.reduce(function(previousVal, currentVal) {
      return previousVal + currentVal;
    }, 0);
    
    1. The filter method is used to iterate through an array and filter out elements where a given condition is not true.
    var newArray = oldArray.filter(function(val){
          return val<6;
    });
    
    1. sort:
      sort can be passed a compare function as a callback. The compare function should return a negative number if a should be before b, a positive number if a should be after b, or 0 if they are equal.
    array.sort(function(a, b) {
          return a - b;
    });
    //smallest to biggest
    
    1. array.reverse();
    2. newArray = oldArray.concat(otherArray);
    3. string.split(' ') 参数为分隔符
    4. arr.join(' ')参数为连接符

    over。下一部分居然是算法题。。。


    words:

    Truncate
    quotient
    Convert Celsius to Fahrenheit 摄氏度--华氏度

    相关文章

      网友评论

        本文标题:JavaScript--FreeCodeCamp

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