https://www.codewars.com/kata/56676e8fabd2d1ff3000000c
Codewars - Achieve mastery through coding practice and developer mentorship
A coding practice website for all programming levels – Join a community of over 3 million developers and improve your coding skills in over 55 programming languages!
www.codewars.com
DESCRIPTION:
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"
Note: In COBOL, it should return "found the needle at position 6"
코드
function findNeedle(haystack) {
let index = -1;
for(let i = 0; i < haystack.length; i++){
if(haystack[i] === 'needle') index = i;
}
return "found the needle at position " + index;
}
'알고리즘 > Codewars' 카테고리의 다른 글
[Codewars] Split Strings (6 kyu) / JavaScript (0) | 2023.01.06 |
---|---|
[Codewars] Find the unique number (6 kyu) / JavaScript (0) | 2023.01.05 |
[Codewars] Beginner - Lost Without a Map(8 kyu) / JavaScript (0) | 2023.01.03 |
[Codewars] String ends with? (7 kyu) / JavaScript (1) | 2023.01.02 |
[Codewars] Moving Zeros To The End (5 kyu) / JavaScript (0) | 2022.12.31 |
댓글