0

I'm trying to take a user's prompt to find the index position of an item in an array and retrieve the value of that same index position in a second array.

var arrayStars = ["Polaris", "Aldebaran", "Deneb", "Vega", "Altair", "Dubhe", "Regulus"];
var arrayConstellations = ["Ursa Minor", "Taurus", "Cygnus", "Lyra", "Aquila", "Ursa Minor", "Leo"];

function starSearch(param) {
var matchingConstellation = arrayConstellations[arrayStars.indexOf("param")];
return matchingConstellation;
  }

var userInput = prompt("Enter a star name");
starSearch(userInput);

When I enter text in the prompt there is no response.

I've tried logging to the console after every line to see what output there is if any and it all seems to work but for some reason it doesn't give the intended result.

I've even replaced one of the array values into the line

var matchingConstellation = arrayConstellations[arrayStars.indexOf("Vega")];

And that returns the proper index item from the second array. It just doesn't seem to display a result when everything is together.

1 Answer 1

1

remove the quotes

arrayStars.indexOf("param")

to

arrayStars.indexOf(param)

you're searching for the string literal param, not whatever the variable param contains

FIDDLE

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you so much, that solved it. Apart from the suggestion I was totally mucking up with displaying it to the screen. Only spent 1.5 days so far trying to figure it out.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.