zoukankan      html  css  js  c++  java
  • 在freecodecamp遇到的题(一)

     1.函数 lookUp 有两个预定义参数:firstName值和prop属性 。

    函数将会检查通讯录是否存在一个联系人的firstName属性等于firstName值,还会检查对应联系人是否存在 prop属性。

    如果它们都存在,函数返回prop属性对应的值。

    如果firstName 值不存在,返回 "No such contact"

    如果prop 属性不存在,返回 "No such property"

    //Setup
    var contacts = [
        {
            "firstName": "Akira",
            "lastName": "Laine",
            "number": "0543236543",
            "likes": ["Pizza", "Coding", "Brownie Points"]
        },
        {
            "firstName": "Harry",
            "lastName": "Potter",
            "number": "0994372684",
            "likes": ["Hogwarts", "Magic", "Hagrid"]
        },
        {
            "firstName": "Sherlock",
            "lastName": "Holmes",
            "number": "0487345643",
            "likes": ["Intriguing Cases", "Violin"]
        },
        {
            "firstName": "Kristian",
            "lastName": "Vos",
            "number": "unknown",
            "likes": ["Javascript", "Gaming", "Foxes"]
        }
    ];
    
    function lookUp(firstName, prop){
    // Only change code below this line
      var _firstname = false,_prop = false;
      for(var i = 0; i < contacts.length; i++){
        if(firstName == contacts[i].firstName && contacts[i][prop]){
          return contacts[i][prop];
        }
        if(contacts[i].firstName == firstName){
          _firstname = true;
          if(contacts[i][prop]){
            _prop = true;
          }
        }
      }
      if(!_firstname){return "No such contact";}
      if(!_prop){return "No such property";}
      // Only change code above this line
    }
    // Change these values to test your function
    lookUp("Kristian", "lastName");
    

    2.将给定的数字转换成罗马数字。

    function convert(num) {
      var romanArr = ["I","V","X","L","C","D","M"];
      //               1   5  10  50  100 500 1000
      var str = "",index = 0;
      while(num > 0){
        var roman = num % 10;
        switch(roman){
          case 3:
            str = romanArr[index] + str;
            roman --;
          case 2:
            str = romanArr[index] + str;
            roman --;
            //continue;
          case 1:
            str = romanArr[index] + str;
            break;  
          case 4:
            str = romanArr[index] + str;
            str = romanArr[index + 1] + str;
            break;
          case 8:
            str = romanArr[index] + str;
            roman --;
          case 7:
            str = romanArr[index] + str;
            roman --;
          case 6:
            str = romanArr[index] + str;
            roman --;
            //continue;
           case 5:
            str = romanArr[index + 1] + str;
            break; 
          case 9:
            str = romanArr[index] + str;
            str = romanArr[index + 2] + str;
            break;
          default:
            break;
    
        }
        index += 2;
        num = Math.floor(num / 10);
      }
      return str;
     //return num;
    }
    
    convert(4);
    

    3.

    写一个 function,它遍历一个对象数组(第一个参数)并返回一个包含相匹配的属性-值对(第二个参数)的所有对象的数组。如果返回的数组中包含 source 对象的属性-值对,那么此对象的每一个属性-值对都必须存在于 collection 的对象中。

    例如,如果第一个参数是 [{ first: "Romeo", last: "Montague" }, { first: "Mercutio", last: null }, { first: "Tybalt", last: "Capulet" }],第二个参数是 { last: "Capulet" },那么你必须从数组(第一个参数)返回其中的第三个对象,因为它包含了作为第二个参数传递的属性-值对。

    function where(collection, source) {
      var arr = [];
      // What's in a name?
      var pro = Object.keys(source);
      //遍历colletion
      for(var j = 0;j < collection.length; j++){
        //遍历source里面的对象
        var boo = true;
        for(var i in source){
          if(source[i] != collection[j][i] || !collection[j].hasOwnProperty(i)){
            boo = false;
          }
        }
        /*for(var i = 0;i < pro.length;i++){
          if(!collection[j].hasOwnProperty(pro[i]) || collection[j][pro[i]] != source[pro[i]]){
            boo = false;
          }
        }*/
        if(boo){
          arr.push(collection[j]);
        }
      }
      return arr;
    }
    
    where([{ "a": 1, "b": 2 }, { "a": 1 }, { "a": 1, "b": 2, "c": 2 }], { "a": 1, "b": 2 });

    老版本浏览器不支持for in,所以项目中尽量不要使用

      

      



  • 相关阅读:
    C++结构体中sizeof
    sizeof()的用法
    XStream和Json
    省市联动
    ajax
    配置文件的读取
    JSP标签库
    字符串函数参数传入传出(去空格)
    字符串函数参数传入传出(字符串反转)
    opendir,readdir,closedir
  • 原文地址:https://www.cnblogs.com/nullman/p/6543534.html
Copyright © 2011-2022 走看看