• W3CSchool闯关笔记(中级脚本算法)


    坚持下去,编程是一门艺术,与君共勉!!!

     1 function sumAll(arr) {
     2     var result = 0;
     3     var sn = Math.min(arr[0] , arr[1]);
     4     var en = Math.max(arr[0] , arr[1]);
     5 /************使用reduce只能在数组元素之间求和,对于数组中没有的数操作不了 
     6  * 如果享用这种方法解决问题需要把两个数字之间包括这两个数字全部存进一个新数组里对新数组求和
     7  *     var newArr = [];
     8     for (var i = 0; i <= en - sn; i++) {
     9         newArr[i] = i + sn;
    10     }
    11     result = newArr.reduce(function(prev ,cur){
    12         return prev + cur;
    13     },0);
    14 */
    15     for (var i = sn; i <= en; i++) {    //黑猫白猫,能抓老鼠就是好猫
    16         result += i;
    17     }
    18 return result;
    19 }
    20 
    21 sumAll([1, 5]);
    1-区间求值
     1 function diff(arr1, arr2) {
     2     var newArr = [];
     3 
     4     pick(arr1,arr2);    //两个数组都要遍历,算法写起来容易,但是时间复杂度很大
     5     pick(arr2,arr1);
     6     function pick(a1,a2){   //编写函数将两个数组中不同的元素放入新数组
     7         for (var i in a1) {
     8             if (a2.indexOf(a1[i]) < 0) {
     9                 newArr.push(a1[i]);
    10             }
    11         }
    12     }
    13 return newArr;
    14 }
    15 
    16 diff([1, 2, 3, 5], [1, 2, 3, 4, 5]);
    2-找出数组差异
     1 function convert(num) {
     2     var one = ["I","II","III","IV","V","VI","VII","VIII","IX"];
     3     var ten = ["X","XX","XXX","XL","L","LX","LXX","LXXX","XC"];
     4     var hundred = ["C","CC","CCC","CD","D","DC","DCC","DCCC","CM"];
     5     var thousand = "M";
     6     var result = "";
     7     var th = Math.floor(num / 1000);
     8     var h = Math.floor((num % 1000) / 100);
     9     var t = Math.floor((num % 100) / 10);
    10     var o = Math.floor(num % 10);
    11     for (var i = 0; i < th; i++) {
    12         result += thousand;
    13     }
    14     if(h){
    15         result += hundred[h - 1];
    16     }
    17     if(t){
    18         result += ten[t - 1];
    19     }
    20     if(o){
    21         result += one[o - 1];
    22     }
    23     
    24  return result;
    25 }
    26 
    27 convert(36);
    3-数字转罗马数字
     1 function where(collection, source) {
     2 
     3 
     4 /*******网友给出的更优解*********
     5     var arr = [] ;
     6     var status = null;
     7     for (var i = 0; i < collection.length; i++) {
     8         for(var imp in source){
     9             if (source[imp] !== collection[i][imp]) {
    10                 status = 0;
    11                 break;
    12             }
    13             status = 1;
    14         }
    15         if(status == 1){
    16             arr.push(collection[i]);
    17         }
    18     }
    19 **********/
    20     
    21     // console.log(Object.getOwnPropertyNames(source).length); 
    22 //获取对象属性个数 
    23     var arr = [], index = 0;
    24     for (; index < collection.length; index++) { 
    25         for (var key in collection[index]) { 
    26             var count = 0; 
    27             for (var key2 in source) { 
    28                 if (collection[index].hasOwnProperty(key2)) { 
    29                     if (source[key2] == collection[index][key2]) { 
    30                         count++; 
    31                     } 
    32                     if (count == Object.getOwnPropertyNames(source).length && key == key2) { 
    33                         arr.push(collection[index]); 
    34                     } 
    35                 } 
    36             } 
    37         } 
    38     } 
    39 return arr;
    40 }
    41 
    42 where([{ first: "Romeo", last: "Montague" }, { first: "Mercutio", last: null }, { first: "Tybalt", last: "Capulet" }], { last: "Capulet" });
    4-对象搜索
     1 function myReplace(str, before, after) {
     2     /*
     3     *本方法只适用于字符串首字母大写的情况
     4     *如果涉及到目标字符串中其他字符大写则需要对temp数组进行遍历逐一比对
     5     *在if外层嵌套for循环并设置temp数组中的索引变量即可解决
     6     */
     7     var arr = str.split(" ");   //把字符串按单词转化成数组
     8     var index = arr.indexOf(before);    //找到目标单词在数组中的索引
     9     var temp1 = arr[index].split("");   //数组中的目标单词按字母分割成新数组
    10     if(/[A-Z]/.test(temp1[0])){         //新数组中首字母大写判定
    11         after = after.charAt(0).toUpperCase() + after.slice(1); //把用来替换的字符串首字符改成大写
    12     }
    13     arr[index] = arr[index].replace(arr[index] , after);    //字符串替换
    14     str = arr.join(" ");    //数组中的单词以空格为间隔拼接成字符串
    15     return str;
    16 }
    17 
    18 myReplace("Let us go to the store", "store", "mall");
    5-字符串查询替换
     1 function translate(str) {
     2     var vowel = ["a", "e", "i", "o", "u"];
     3     if (vowel.indexOf(str[0]) != -1) {
     4         return str + "way";
     5     }
     6     while (vowel.indexOf(str[0]) == -1) {
     7         str = str.substr(1) + str.substr(0, 1);
     8     }
     9     return str + "ay";
    10 }
    11 
    12 translate("consonant");
    6-字符串移动插入
     1 function pair(str) {
     2     var arr = new Array();  //注意:这里的数组定义方式是实例化定义
     3     str = str.split("");
     4     for (var i in str) {
     5         arr[i] = new Array();
     6         arr[i].push(str[i]);
     7         if (str[i] == "G" || str[i] == "C") {
     8             (arr[i] == "G") ? arr[i].push("C") : arr[i].push("G");
     9         } else {
    10             (arr[i] == "A") ? arr[i].push("T") : arr[i].push("A");
    11         }
    12     }
    13     return arr;
    14 }
    15 
    16 pair("GCG");
    7-字符配对
    8
     1 function fearNotLetter(str) {
     2 /*******下面这种网友给出的方法只符合缺失一个字符的情况
     3  * 如果缺失多个则只能输出缺失的第一个
     4     var sub;
     5     for (var index = 0; index < str.length - 1; index++){ 
     6         sub = str.charCodeAt(index + 1) - str.charCodeAt(index);
     7         if (sub == 1) { 
     8             if (index == str.length) { 
     9                 return undefined; 
    10             } 
    11         } else { 
    12             return String.fromCharCode(str.charCodeAt(index) + 1); 
    13         } 
    14     }
    15 */
    16 
    17 //下面本人字节写的可以返回缺失一段多个字符的情况,但是不符合缺失多端字符的情况如abeg
    18 //缺失多段字符的方法可以将内层for循环写成功能函数,在外层循环中每当发现sub!=1时去调用即可
    19 //调用结束过后使用continue让外层循环继续执行直到整个str都遍历完为止
    20     var sub = 0;
    21     var index = [];
    22     for (var i = 0; i < str.length - 1; i++) {
    23         sub = str.charCodeAt(i + 1) - str.charCodeAt(i);
    24         if (sub == 1) {
    25             if(i == str.length){
    26                 return undefined;
    27             }
    28         } else {
    29             for(var j = 0 ; j < sub - 1; j++){
    30                 index[j] = String.fromCharCode(str.charCodeAt(i)+1+j);
    31                 return index[j];
    32             }
    33         }
    34     }
    35 }
    36 
    37 fearNotLetter("abeg");
    -字符串查询补充
    9
     1 function boo(bool) {
     2 // What is the new fad diet for ghost developers? The Boolean.
     3 //这种情况只能做检查提示,开发过程中除了检查提示还应该让用户重新输入直到正确为止
     4 //可以使用try  catch结构处理输入异常,或使用while函数让用户输入直到输入正确为止
     5     if (bool === true || bool === false) {
     6         return true;
     7     } else {
     8         return false;
     9     }
    10 }
    11 
    12 boo(null);
    -输入检查
    10
     1 function unite(arr1, arr2, arr3) {
     2 
     3     var args = Array.from(arguments);
     4     var arr = args.reduce(function(prev,cur){
     5         return prev.concat(cur);
     6     });
     7     return arr.filter(function(item,index,arr){
     8         return arr.indexOf(item) === index;  
     9     });
    10 }
    11 
    12 unite([1, 3, 2], [5, 2, 1, 4], [2, 1]);
    -数组去重
     1 function convert(str) {
     2 // &colon;&rpar;
     3     var arr = str.split("");
     4     for (var i = 0; i < str.length; i++) {
     5         switch (arr[i]) {
     6             case '&':
     7                 arr[i] = "&amp;";
     8                 break;
     9             case '<':
    10                 arr[i] = "&lt;";
    11                 break;
    12             case '>':
    13                 arr[i] = "&gt;";
    14                 break;
    15             case '"':
    16                 arr[i] = "&quot;";
    17                 break;
    18             case "'":
    19                 arr[i] = "&apos;";
    20                 break;
    21         }
    22     }
    23     return arr.join("");
    24 }
    25 
    26 convert("Dolce & Gabbana");
    11-html符号转实体
     1 function spinalCase(str) {
     2 
     3   var array=str.split(" ");
     4   if(array.length<=1){
     5     array=str.split("_");
     6   }
     7   if(array.length<=1){
     8     return str.replace(/([A-Z])/g,"-$1").toLowerCase();
     9   }
    10   str=array[0].toLowerCase();
    11   for(var i=1;i<array.length;i++){
    12     str=str.concat('-').concat(array[i].toLowerCase());
    13   }
    14 return str;
    15 }
    16 
    17 spinalCase('This Is Spinal Tap');
    12-字符串连接
     1 function sumFibs(num) {
     2     var fibo = [1, 1];
     3     var nums = 2;
     4     var temp = 0;
     5     while(true){
     6         temp = fibo[0] + fibo[1];
     7         if(temp > num){
     8             return nums;
     9         }
    10         if(temp % 2 !== 0){
    11             nums += temp;
    12         }
    13         fibo[0] = fibo[1];
    14         fibo[1] = temp;
    15     }
    16 }
    17 
    18 sumFibs(1000);
    13-斐波那契
     1 function sumPrimes(num) {
     2     var sum = 0;
     3     for(var i = 2; i <= num; i++){
     4         var j = 2;
     5         for( ; i%j !== 0; j++){}
     6         if(i == j){
     7             sum += i;
     8         }
     9     }
    10     return sum;
    11 }
    12 
    13 sumPrimes(10);
    14-质数求和
  • 相关阅读:
    Solr开发文档(转)
    使用Mybatis-Generator自动生成Dao、Model、Mapping相关文件(转)
    用nodejs搭建最简单、轻量化的http server(转)
    [ASP.NET MVC 小牛之路]03
    【大型网站技术实践】初级篇:借助Nginx搭建反向代理服务器(转)
    我是如何在SQLServer中处理每天四亿三千万记录的(转)
    pycharm Unresolved reference 无法引入包
    vue 学习
    《插件》一个比较好用的 chrome浏览器的json格式化插件
    ip地址正则表达式
  • 原文地址:https://www.cnblogs.com/edward-life/p/10774068.html
Copyright © 2020-2023  润新知