美文网首页freeCodeCamp旅途
freeCodeCamp 旅途10 - 算法实战

freeCodeCamp 旅途10 - 算法实战

作者: HJSD | 来源:发表于2019-10-16 01:00 被阅读0次

    项目实战:回文检查器

    function palindrome(str) {
      return str.replace(/[\W_]/g, '').toLowerCase() ===
                 str.replace(/[\W_]/g, '').toLowerCase().split('').reverse().join('');
    }
    palindrome("eye");
    
    function palindrome(str) {
        let front = 0
        let back = str.length - 1
        while (back > front) {
            if (str[front].match(/[\W_]/)) {
                front++
                continue
            }
            if (str[back].match(/[\W_]/)) {
                back--
                continue
            }
            if (str[front].toLowerCase() !== str[back].toLowerCase()) return false
            front++
            back--
        }
        return true
    }
    

    项目实战:罗马数字转换器

    var convertToRoman = function(num) {
      var decimalValue = [ 1000, 900, 500, 400, 100, 90, 50, 40, 10, 9, 5, 4, 1 ];
      var romanNumeral = [ 'M', 'CM', 'D', 'CD', 'C', 'XC', 'L', 'XL', 'X', 'IX', 'V', 'IV', 'I' ];
      var romanized = '';
      for (var index = 0; index < decimalValue.length; index++) {
        while (decimalValue[index] <= num) {
          romanized += romanNumeral[index];
          num -= decimalValue[index];
        }
      }
      return romanized;
    }
    convertToRoman(36);
    

    项目实战:凯撒密码

    function rot13(str) {
        return str.split('')
        .map.call(str, function(char) {
            let x = char.charCodeAt(0);
            if (x < 65 || x > 90) {
              return String.fromCharCode(x); 
            }
            else if (x < 78) {
              return String.fromCharCode(x + 13);
            }
            return String.fromCharCode(x - 13);
        }).join('');  
      // return str.replace(/[A-Z]/g, L => String.fromCharCode((L.charCodeAt(0) % 26) + 65));
    }
    rot13("SERR PBQR PNZC");
    

    项目实战:电话号码验证器

    function telephoneCheck(str) {
      // Good luck!
      let regex = /^(1\s?)?(\(\d{3}\)|\d{3})[\s\-]?\d{3}[\s\-]?\d{4}$/;
      return regex.test(str);
    }
    telephoneCheck("555-555-5555");
    

    项目实战:收银机

    var denom = [
      { name: 'ONE HUNDRED', val: 100.00},
      { name: 'TWENTY', val: 20.00},
      { name: 'TEN', val: 10.00},
      { name: 'FIVE', val: 5.00},
      { name: 'ONE', val: 1.00},
      { name: 'QUARTER', val: 0.25},
      { name: 'DIME', val: 0.10},
      { name: 'NICKEL', val: 0.05},
      { name: 'PENNY', val: 0.01}
    ];
    function checkCashRegister(price, cash, cid) {
      var output = { status: null, change: [] };
      var change = cash - price;
      var register = cid.reduce(function(acc, curr) {
        acc.total += curr[1];
        acc[curr[0]] = curr[1];
        return acc;
      }, { total: 0 });
      if (register.total === change) {
        output.status = 'CLOSED';
        output.change = cid;
        return output;
      }
      if (register.total < change) {
        output.status = 'INSUFFICIENT_FUNDS';
        return output;
      }
      var change_arr = denom.reduce(function(acc, curr) {
        var value = 0;
        while (register[curr.name] > 0 && change >= curr.val) {
          change -= curr.val;
          register[curr.name] -= curr.val;
          value += curr.val;
          change = Math.round(change * 100) / 100;
        }
        if (value > 0) {
            acc.push([ curr.name, value ]);
        }
        return acc; 
      }, []);
      if (change_arr.length < 1 || change > 0) {
        output.status = 'INSUFFICIENT_FUNDS';
        return output;
      }
      output.status = 'OPEN';
      output.change = change_arr;
      return output;
    }
    checkCashRegister(19.5, 20, [["PENNY", 1.01], ["NICKEL", 2.05], ["DIME", 3.1], ["QUARTER", 4.25], ["ONE", 90], ["FIVE", 55], ["TEN", 20], ["TWENTY", 60], ["ONE HUNDRED", 100]]);
    

    相关文章

      网友评论

        本文标题:freeCodeCamp 旅途10 - 算法实战

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