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

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

输出应该是这样的:

  • Sophomore-Alcohol-Conquer-RockyRoad(阵列)
  • 8(字母出现的次数)
  • 3 - 2 - 1 -2(每个字符串中的字母数)

我必须将数组转换为字符串并扫描每个字符串中出现的字母。对第三个输出的任何帮助将不胜感激!

drahcir88t 回答:有没有办法在不使用任何内置函数的情况下返回字符串数组中出现的字符?

您可以通过数组进行映射并使用减少字符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","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("-"));

,

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

例如:您可以在数组上使用 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","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","RockyRoad"])

本文链接:https://www.f2er.com/2553.html

大家都在问