Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

JavaScript

How to write javascript function properly

I wrote the function getIndexOfColor, and it's intended to take in a color, and return the index of the color in the array colors, and if its not there, then return false. For some reason it always returns negative 1. Here is my code: https://w.trhou.se/w94g45a15k

2 Answers

Steven Parker
Steven Parker
229,670 Points

I see a few issues:

  • on line 7, you need parentheses after the "toLoweCase" method name to invoke it
  • on line 8,you don't need a loop with "indexOf" as you would to search the colors individually
  • on line 9, you have an assignment ("=") instead of an equality test ("==")
  • that test is also not necessary when using "indexOf"
  • on line 10, you return the value from "indexOf" without testing it, so will get -1 instead of "false" when not found

It might actually make more sense to return -1 for not found instead of false, to avoid the potential for misinterpreting the result by having a different type.

Update: fixed the line numbers.

Thanks, but what do you mean for line 7? Wouldn't I want to have them lowercase so "Red" would be the same as "red"?

I fixed it using the assignment here : https://w.trhou.se/k7v83kslip

OK, I edited it.