Skip to content

Commit 8939f58

Browse files
committed
update my answers to find.js
1 parent 48629e0 commit 8939f58

1 file changed

Lines changed: 7 additions & 4 deletions

File tree

Sprint-3/4-stretch/find.js

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,13 @@ 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-
// [MM] - The index variable increments by one each time the letter (str[index]) at the current index does not match the target letter (char)
23+
// The index starts at 0 and increments by one at each iteration as long as the nested if statement is not triggered. In the case of find("code your future", "u"), the if statement is triggered at index 7 because a match is found for letter u at this index. The while loop is exited and the number 7 is returned by the find function. In the case of find("code your future", "z") no match is found by the time the while loop terminates. In this case the find function returns -1 to indicate the letter z was not found in the string
24+
2425
// b) What is the if statement used to check
25-
// [MM] - The if statement is used to check if the current letter matches the target letter (char). If a match is found (str[index] === char) it will cause an early exit from the while loop at the current index
26+
// The if statement is used to check if the current letter in the string (str[index]) matches the target letter (char). If a match is found (str[index] === char) it will cause an early exit from the while loop at the current index which will be returned by the find function
27+
2628
// c) Why is index++ being used?
27-
// [MM] - The index++ increments the index variable by one each time the current letter (str[index]) does not match the target letter (char). It only runs if a match is not found
29+
// index++ is being used because the while loop does not have a built in iterator (like the for loop). Index is initiated at 0 outside the while loop and at each iteration, whenever the if statement is not triggered, index++ adds 1 to the index number
30+
2831
// d) What is the condition index < str.length used for?
29-
// [MM] - The condition index < str.length is used to set the upper bound of the loop (the loop will run while index number is less than str.length)
32+
// The condition index < str.length is used to set the upper bound of the loop (the loop will run while index number is less than str.length and will terminate once index is equal to str.length, causing the while loop to stop. If there is no condition to terminate the while loop it will run infinitely

0 commit comments

Comments
 (0)