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!