Skip to main content
Table of contents

Back to challenges

Challenge 6 - Needle in a Haystack

Can you find the needle in the haystack?

Write a function findNeedle() that takes an array full of junk, but containing one “needle”, which you need to find with a for loop.

After your function finds the needle it should return a message (as a string) that says: "Found the needle at position x", with x being the index number at which you find the needle.

So:

var haystack = ['hay', 'rabbit', 'needle', 'hat'];
findNeedle(haystack);

Should return:

'Found the needle at position 2'

Solution

This page was last reviewed on 3 February 2022. It needs to be reviewed again on 3 February 2023 .
This page was set to be reviewed before 3 February 2023. This might mean the content is out of date.