Skip to content

Commit 85e4ce4

Browse files
committed
Answer all questions about the find while loop in find.js
1 parent 202642d commit 85e4ce4

1 file changed

Lines changed: 14 additions & 4 deletions

File tree

Sprint-3/4-stretch/find.js

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,17 @@ console.log(find("code your future", "z"));
1919
// Use the Python Visualiser to help you play computer with this example and observe how this code is executed
2020
// Pay particular attention to the following:
2121

22-
// a) How the index variable updates during the call to find
23-
// b) What is the if statement used to check
24-
// c) Why is index++ being used?
25-
// d) What is the condition index < str.length used for?
22+
// a) index starts at 0 and increments by 1 on each iteration until either the target
23+
// character is found (returns early) or index reaches str.length (loop ends).
24+
// For find("code your future", "u"): index goes 0,1,2,3,4,5,6,7 — "u" found at index 7.
25+
// For find("code your future", "z"): index goes 0..15, never matches, returns -1.
26+
27+
// b) The if statement checks whether the character at the current index equals the
28+
// target character `char`. If it does, the function returns that index immediately.
29+
30+
// c) index++ increments the index by 1 after each iteration so the loop moves forward
31+
// through the string one character at a time, preventing an infinite loop.
32+
33+
// d) index < str.length is the loop condition. It ensures the loop only runs while
34+
// index is within the valid range of the string. Once index equals str.length
35+
// there are no more characters to check, so the loop stops and -1 is returned.

0 commit comments

Comments
 (0)