File tree Expand file tree Collapse file tree 1 file changed +11
-3
lines changed
Sprint-3/revise/investigate Expand file tree Collapse file tree 1 file changed +11
-3
lines changed Original file line number Diff line number Diff line change 11function find ( str , char ) {
2+ //this var will store once find the index === to equal.
23 let index = 0 ;
34
5+ // The while loop iterates through each character in the string
6+ //while loop will map the string and find if there is any match character in the string.
47 while ( index < str . length ) {
58 if ( str [ index ] === char ) {
9+ // If the character at the current index matches the input char, return the index
610 return index ;
711 }
12+ //update the value of index and continue until index is less than the str.length.
813 index ++ ;
914 }
15+ // If the loop completes and no match is found, return -1
1016 return - 1 ;
1117}
1218
13- console . log ( find ( "code your future" , "u" ) ) ;
14- console . log ( find ( "code your future" , "z" ) ) ;
19+ console . log ( find ( "hello word find my index" , "x" ) ) ; // Output: 23
20+ console . log ( find ( "code your future" , "f" ) ) ; // Output: 10
21+ console . log ( find ( "code your future" , "z" ) ) ; // Output: -1
1522
16- // The while loop statement allows us to do iteration - the repetition of a certain number of tasks according to some condition
23+ // The while loop statement allows us to do iteration - the repetition of a certain number
24+ // of tasks according to some condition
1725// See the docs https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/while
1826
1927// Use the Python Visualiser to help you play computer with this example and observe how this code is executed
You can’t perform that action at this time.
0 commit comments