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>
  • 相关阅读:
    pl/sql配置-本地不安装oracle-登录pl/sql
    js中查看js对象的属性
    eos中nui提交表单
    Window clearTimeout() 方法与setTimeout
    小程序---app.json文件的配置,全局的,对所有页面都适用
    css--加载中样式
    Vue+axios请求本地json
    vue中通过方法返回data中的对象是这个{__ob__: Observer},怎么处理 呢???
    解决 canvas 绘图在高清屏中的模糊问题
    总结:活动类开发必知必会
  • 原文地址:https://www.cnblogs.com/dtdxrk/p/2720823.html
Copyright © 2011-2022 走看看