Sovi.AI - AI Math Tutor

Scan to solve math questions

QUESTION IMAGE

jump to level 1 loop through the characters in the string oldproverb an…

Question

jump to level 1

loop through the characters in the string oldproverb and assign timesoccurred with the number of times i appears in the string.

note: your code will be tested with more values for oldproverb.

1 let oldproverb = \all good things must come to an end.\;
2 let timesoccurred = 0;
3
4 / your solution goes here /
5

Explanation:

Step1: Initialize a loop

Use a for loop to iterate through each character in the oldProverb string.

for (let i = 0; i < oldProverb.length; i++) {

Step2: Check for 'i'

Inside the loop, check if the current character (accessed via oldProverb[i]) is equal to 'i'. If it is, increment the timesOccurred variable.

  if (oldProverb[i] === 'i') {
    timesOccurred++;
  }
}

Answer:

let oldProverb = "All good things must come to an end.";
let timesOccurred = 0;
for (let i = 0; i < oldProverb.length; i++) {
  if (oldProverb[i] === 'i') {
    timesOccurred++;
  }
}