Help with map function!

Hey guys!

I’ve been playing around with the map function and I thought I had it down, but I guess not.
I’m having some trouble with the output of my map function.

Here’s an example of what should happen.

correctStream(
  ["it", "is", "find"],
  ["it", "is", "fine"]
) ➞ [1, 1, -1]

1 for true and -1 for false. Depending on whether userInput = correctInput

The code I have so far is

function correctStream(user, correct) {
	return user.map((word, i) => word[i] == correct[i]) ? 1 : -1;
	}

All this does is return ‘1’ . Nothing else. Does anyone know why this is happening? I thought the map function would test for every element and return a value for each test.

Thanks!

Yes that’s true, but your code doesn’t check each element/ word, but word[i], which is only one letter of the word (the ith).

This should work:

function correctStream(user, correct) {
	return user.map((word, i) => word == correct[i]? 1 : -1);
}

Ive tried this but it still only returns just [1] . Not sure why it’s doing this!

But even when I replace word[i] with just word I get the same result. I do understand what you’re saying but I don’t get why I’m getting the same result

You closed the map function in wrong place

put ) after -1

1 Like