zoukankan      html  css  js  c++  java
  • JavaScript测试题练习

    1、给你一个字符串,要你找出里面出现次数最多的字母和出现的次数,例如:“weruiewiuorsdjfklxsdfwerxcvcvcvmqwersadftioytyiopuioptuioptyiouio”;

     1  <script type="text/javascript">
     2     var str = "weruiewiuorsdjfklxsdfwerxcvcvcvmqwersadftioytyiopuioptuioptyiouio",
     3         copyStr = [],
     4         i = 0;
     5     var n = "";
     6 
     7     while(str.charAt(0)){
     8         copyStr[i] = str.charAt(0) + "=" + (str.split(str.charAt(0)).length-1);
     9         str = str.split(str.charAt(0)).join("");
    10         i++;
    11     }
    12 
    13     for(var j=0, temp=0; j<copyStr.length; j++){
    14         if(temp <= Number(copyStr[j].split("=")[1])){
    15             temp = Number(copyStr[j].split("=")[1]);
    16             n = copyStr[j];
    17         }
    18     }
    19    alert("出现次数最多的为:"+ n +"\n" +"每个字母出现的次数为:"+ copyStr);
    20 
    21  </script>

    2、字符串的字节长度;

     1 <script type="text/javascript">
     2     function strLength(s){
     3         if(!arguments.length) {
     4             alert("没有参数!");
     5             return;
     6         }
     7         if(arguments.length>1) {
     8             alert("参数过多只能设置一个!");
     9             return;
    10         }
    11         if(s==""){
    12             alert("参数不能为空!");
    13             return;
    14         }
    15 
    16         var n=0;
    17         for(var i=0; i<s.length; i++){
    18             if(s.charCodeAt(i)>255){    //charCodeAt() 方法可返回指定位置的字符的 Unicode 编码
    19                 n+=2;
    20             }else{
    21                 n++;
    22             }
    23         }  
    24         alert(n);
    25     }
    26     strLength("节长度")
    27  </script>

    3.、去掉数组中重复的元素 var a = ["abc", "def", "a", "b", "c", "a", "b", "c"];

     1 <script type="text/javascript">
     2 var a = ["abc", "def", "a", "b", "c", "a", "b", "c"];
     3 
     4 function strip(str){
     5     if(str.length<2) return str;
     6 
     7     var arr = [];
     8     for(var i=0; i<str.length; i++){
     9         arr.push(str.splice(i--,1));  //给arr添加str数组的第一个
    10         for(var j=0; j<str.length; j++){
    11             if(str[j] == arr[arr.length-1]){    //对比arr数组的最后一个
    12                 str.splice(j,1);
    13             }
    14         }
    15     }
    16     alert(arr);
    17 }
    18 
    19 strip(a);
    20 
    21  </script>
  • 相关阅读:
    Python 爬邮箱
    Attempting to fetch value instead of handling error Internal: failed to get device attribute 13 for device 0: CUDA_ERROR_UNKNOWN: unknown error
    Ubuntu使用TinyOS常见错误
    Ubuntu16.04中安装TinyOS
    cmake 常用命令
    微信开发小结-PHP
    KVC&KVO&NSNotification
    IOS 图片拉伸技巧
    IOS学习-报错误 Finishing up a navigation transition in an unexpected state. Navigation Bar subview tree might get corrupted.
    小程序订阅消息(模板消息已被废弃)
  • 原文地址:https://www.cnblogs.com/dtdxrk/p/2720823.html
Copyright © 2011-2022 走看看