This repository was archived by the owner on Apr 18, 2025. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 49
Expand file tree
/
Copy pathfind.js
More file actions
29 lines (24 loc) · 1.19 KB
/
find.js
File metadata and controls
29 lines (24 loc) · 1.19 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
function find(str, char) {
let index = 0;
while (index < str.length) {
if (str[index] === char) {
return index;
}
index++;
}
return -1;
}
console.log(find("code your future", "u"));
console.log(find("code your future", "z"));
// The while loop statement allows us to do iteration - the repetition of a certain number of tasks according to some condition
// See the docs https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/while
// Use the Python Visualiser to help you play computer with this example and observe how this code is executed
// Pay particular attention to the following:
// a) How the index variable updates during the call to find
//Ans. It adds 1 everytime the loop runs as index keep adding up until the input character has been reached
// b) What is the if statement used to check
//Ans. it's checking the value of certain index or string against the input character
// c) Why is index++ being used?
// Ans. It is being used to just add 1 to the value of index each time it has not reached the input character
// d) What is the condition index < str.length used for?
//Ans. As long as number of index is less than the length of the string.