有没有办法在不使用任何内置函数的情况下在字符串数组中出现 return 个字符?

Is there a way to return character occurrences in a string array without using any built in functions?

我的函数有问题,我需要查找字符在数组中的字符串中出现的次数。我们不允许使用任何内置函数,因此 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"]);

输出应如下所示:

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

您可以映射数组并使用减少字符出现的总次数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}

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("-"));

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

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

您还应该使用 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"])