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 JavaScript Loops, Arrays and Objects Tracking Data Using Objects The Student Record Search Challenge

Tyler Dix
Tyler Dix
14,230 Points

I'm embarrassed to even ask, but...

I can't figure this out. I've broken this down in to the simplest possible program just to get the fundamentals, and it isn't working.

I've added notes on what I expect to happen. I've also linked the workspace if you want to open the whole thing.

Right now, all I get is a user prompt (which is a start!). Everything else doesn't work as expected, and I have no errors in the console to debug.

I'm embarrassed to ask this because it's so simple, but I'm stuck.

Thanks,

Tyler

https://w.trhou.se/e1gppuhb7g

var simpleArray = ['red', 'green', 'blue'] // doesn't get easier than this

var message = ''; // store empty value in message variable to avoid "undefined" errors. I don't think I'm interacting with this variable at all, but I included it here anyway.

// pre-formats the print message
function print(message) {
  var outputDiv = document.getElementById('output');
  outputDiv.innerHTML = message;
}

function runProgram(){ // start program with no parameters. Not clear why I need to start a program here, other than "tell the JavaScript interpreter to do something." Doesn't calling the print function "do something?" 
  var input = prompt("Name one of the RGB values: "); // prompt user and store response in 'input' variable
  for (var i = 0; i < simpleArray; i += 1) { // cycle through the simple array
    if (input === simpleArray[i]) { // check the input of user against each array string value
      print("Yep, " + input + " is one of those values!"); // prints if the condition is true
    } else {
      print("Nope, try again.");
    } // ends else statement
  } // ends for loop
} // ends function

runProgram(); // runs program

2 Answers

I made the following modification:

for (var i = 0; i < simpleArray; i += 1) { // 

should be

for (var i = 0; i < simpleArray.length; i += 1) { // 

and then two added a break statement after a correct answer

print("Yep, " + input + " is one of those values!"); // prints if the condition is true      
       break;

Without the break your loop will always run through all colors and therefore the only successful answer would be the last one (blue).

Tyler Dix
Tyler Dix
14,230 Points

Perfect! Thank you so much, Kris. I made these changes and now it works.

I think I've got the fundamentals I need to tackle the challenge (I hope). I really appreciate it. (And I'm glad I'm not too far off.)

i think kris is spot on. also i would add .toLowerCase() for input incase user enters capital letters

    if (input.toLowerCase() === simpleArray[i]) { // check the input of user against each array string value