Skip to content

Commit d04bbba

Browse files
committed
added some comments in the function
1 parent 801006e commit d04bbba

File tree

1 file changed

+11
-3
lines changed

1 file changed

+11
-3
lines changed

Sprint-3/revise/investigate/find.js

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,27 @@
11
function 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

0 commit comments

Comments
 (0)