11function find ( str , char ) {
22 let index = 0 ;
3-
43 while ( index < str . length ) {
54 if ( str [ index ] === char ) {
65 return index ;
76 }
87 index ++ ;
98 }
9+
1010 return - 1 ;
1111}
1212
13- console . log ( find ( "code your future" , "u" ) ) ;
14- console . log ( find ( "code your future" , "z" ) ) ;
13+ // console.log(find("code your future", "u"));
14+ // console.log(find("code your future", "z"));
1515
1616// The while loop statement allows us to do iteration - the repetition of a certain number of tasks according to some condition
1717// See the docs https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/while
@@ -20,6 +20,11 @@ console.log(find("code your future", "z"));
2020// Pay particular attention to the following:
2121
2222// a) How the index variable updates during the call to find
23+ // It will keep track of our current position in the string starting from 0 and increases by 1 on each loop iteration
24+ // intil character is found or the loop reache the end of the string
2325// b) What is the if statement used to check
26+ // It is checking if the character at the current index in str is equal to char.
2427// c) Why is index++ being used?
28+ //It is used to update the index during each iteration so the loop moves to the next character.
2529// d) What is the condition index < str.length used for?
30+ // This ensure the loop keeps running until index reaches the length of the string, so we do not go past the last character.
0 commit comments