【问题标题】:Is there a way to return character occurrences in a string array without using any built in functions?有没有办法在不使用任何内置函数的情况下返回字符串数组中出现的字符?
【发布时间】:2021-10-07 02:05:38
【问题描述】:

我的函数出现问题,我需要找出字符在数组内的字符串中出现的次数。我们不允许使用任何内置函数,因此 array.toString() 是不可能的。到目前为止,这是我想出的代码:

function occur(arr){
    let strings = "";
    let result1 = "";
    let output = "";
    let display = "";
    let counter = 0;
    
    // Convert array to string
    for (let i = 0; i < arr.length; i++){
        strings = arr[i];
        display += arr[i] + (i < arr.length - 1? "-": "");


        // Convert string to letters
        for (let j = 0; j < strings.length; j++){
            result1 = strings[j];

            // Scans letters for match
            for (let x = 0; x < result1.length; x++){
                if (result1 === "o"){
                    counter++;
                    output +=counter + (j > arr.length? "-": "");
                 }
            }
        }
    }
    console.log(display);
    console.log(counter);
    console.log(output);
}

occur(["Sophomore", "Alcohol", "Conquer", "RockyRoad"]);

输出应该是这样的:

  • 大二-酒精-征服-洛基路(阵列)
  • 8(字母出现的次数)
  • 3 - 2 - 1 -2(每个字符串中的字母数)

我必须将数组转换为字符串并扫描每个字符串以查找出现的字母。非常感谢有关第三个输出的任何帮助!

【问题讨论】:

    标签: javascript arrays string loops find-occurrences


    【解决方案1】:

    您应该利用该语言。语言提供了太多我们不应该自己编写的功能。

    例如:您可以在数组上使用 join 函数来连接字符串。

    您还应该使用高阶函数,例如 map 和 reduce,而不是 for 循环。

    最后一件事是尝试概括解决方案。因此,您可以在函数中传递字符,而不是寻找 'o'。 这是我的解决方案

    const counter = (arr,char) =>{
        const result1 = arr.join('-');
        const count = arr.map((str) =>{
          const total_occurance = str.split('').reduce((count,character) => {
              if(character === char){
                  return count+1;
              }
              return count;
          },0);
          return total_occurance;
        });
        const result2 = count.reduce((totalCount,value) => totalCount+value,0);
        const result3 = count.join('-');
        console.log({result1,result2,result3});
    
    }
    
    counter(["Sophomore", "Alcohol", "Conquer", "RockyRoad"],'o');

    因为您不允许使用内置函数。你可以用这个。

    function occur(arr){
        let strings = "";
        let result1 = "";
        let output = "";
        let display = "";
        let total_count=0; 
        // Convert array to string
        for (let i = 0; i < arr.length; i++){
            strings = arr[i];
            display += arr[i] + (i < arr.length - 1? "-": "");
            let counter = 0;
            // Convert string to letters
            for (let j = 0; j < strings.length; j++){
                result1 = strings[j];
                // Scans letters for match
            
                    if (result1 === "o"){
                        counter++;
                        total_count++;
                    }
                
                }
                output +=counter + (i < arr.length-1? "-": "");
        }
        console.log(display);
        console.log(total_count);
        console.log(output);
    }
    
    occur(["Sophomore", "Alcohol", "Conquer", "RockyRoad"])

    【讨论】:

    • 很抱歉最后一分钟编辑它,但我们不允许使用任何内置函数,如 split 或 join,所以我必须使用循环和条件语句。非常感谢您的帮助!
    • 我添加了另一个只使用 for 循环的版本
    • 我花时间处理我的代码,我刚刚意识到我的第二个 for 循环并不是真正需要的,因为我可以直接比较 string[i] === "o"。再次感谢您帮助我!
    【解决方案2】:

    let arr = ["Sophomore", "Alcohol", "Conquer", "RockyRoad"];
    let counter = 0;
    let display = arr.join("-");
    let result = arr.map(x => {
      let count = x.match(/o/ig)?.length;
      counter += count;
      return count;
    })
    
    console.log(display);
    console.log(counter);
    console.log(result.join("-"));

    【讨论】:

      【解决方案3】:

      您可以映射数组并使用减少字符o 出现的总数。最后,使用join 将带有- 符号的字符串粘合起来。

      const arr = ["Sophomore", "Alcohol", "Conquer", "RockyRoad"];
      const counts = arr.map(
        item => item.split('')
          .reduce((count, char) => {
            return char === 'o' ? ++count : count;
          }, 0)
      );
      
      console.log('Output: ', arr.join('-'));
      console.log('Total: ', counts.reduce((acc, curr) => acc + curr));
      console.log('Each String: ', counts.join('-'));
      .as-console-wrapper{min-height: 100%!important; top: 0}

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2015-06-27
        • 2021-04-26
        • 2017-06-04
        • 1970-01-01
        • 2019-02-20
        • 2019-03-22
        • 1970-01-01
        相关资源
        最近更新 更多