Beginners JavaScript
  • Welcome!
  • Tutorial
    • Step 1: Hello World
    • Step 2: Data Types
    • Step 3: Comments
    • Step 4: Variables
    • Step 5: If/Else Statements
    • Step 6: For Loops
    • Step 7: Functions
    • Step 8: Arrays
    • Step 9: Objects
  • Challenges
    • Challenge 1: Age Difference
    • Challenge 2: Famous Writers
    • Challenge 3: Fix the Code
    • Challenge 4: FizzBuzz
    • Challenge 5: Needle in a Haystack
Powered by GitBook
On this page
  1. Challenges

Challenge 5: 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'
PreviousChallenge 4: FizzBuzz

Last updated 6 years ago