0

I'm having troubles with my function where I need to find how many times the character appears in the strings inside an array. We are not allowed to use any built in functions so array.toString() is not possible. So far, this is the code I came up with:

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

and the output should look like this:

  • Sophomore-Alcohol-Conquer-RockyRoad (Array)
  • 8 (Number of times the letter appeared)
  • 3 - 2 - 1 -2 (Number of letters in each string)

I got to convert the array to strings and scanned each string for letter occurrences. Any help with regards to the third output would be highly appreciated!

YabaiTV
  • 5
  • 4

3 Answers3

0

You can map through the array and use reduce the count the total number of occurrences of the character o. And finally, use join to glue the strings with - sign.

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}
Sajeeb Ahamed
  • 6,070
  • 2
  • 21
  • 30
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("-"));
Harsh Saini
  • 598
  • 4
  • 13
0

You should leverage the language. Language provide so many functions that we shouldn't write by ourself.

for eg: you can use join function on array to join the strings.

you should also use higher order functions like map and reduce instead of for loops.

one last thing try to generalize the solution. so instead of looking for 'o' you can pass the character in the function. Here is my solution

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');

since you are not allowed to use built in functions. you can use this.

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"])
Ankit Aabad
  • 115
  • 1
  • 4
  • Sorry to edit it last minute but we are not allowed to use any built in functions like split or join so I have to use loops and conditional statements. I really appreciate your help! – YabaiTV Aug 01 '21 at 10:45
  • I've added another version that uses just for loops – Ankit Aabad Aug 01 '21 at 11:00
  • 1
    I've spent time working on my code and I just realized that my second for loop wasn't really needed since I can just directly compare string[i] === "o". Thanks again for helping me out! – YabaiTV Aug 01 '21 at 11:28