-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtask52.js
25 lines (20 loc) · 844 Bytes
/
task52.js
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
//A Needle in the Haystack
/*Can you find the needle in the haystack?
Write a function findNeedle() that takes an array full of junk but containing one "needle"
After your function finds the needle it should return a message (as a string) that says:
"found the needle at position " plus the index it found the needle, so:
Example(Input --> Output)
["hay", "junk", "hay", "hay", "moreJunk", "needle", "randomJunk"] --> "found the needle at position 5"
["hay", "junk", "hay", "hay", "moreJunk", "needle", "randomJunk"] --> "found the needle at position 5"
*/
function findNeedle(haystack) {
return "found the needle at position " + haystack.indexOf("needle");
}
/*
function findNeedle(haystack) {
for (let i = 0; i < haystack.length; i++)
if (haystack[i] === "needle") {
return "found the needle at position "+ i;
}
}
*/