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

Experimenting with Loops, Arrays and Conditions...

Hello all,

Well I have gone from knowing nothing on code (let alone JavaScript), to the end off the Arrays section with Dave within little over 3 weeks, and thought it was about time to slow down and experiment a little bit to try and gain a better understanding. So can all please look over my code for a simple guessing game and see what you think. Please be gentle! :-)

var answers = ['bashful', 'doc', 'dopey', 'happy', 'sleepy', 'sneezy', 'grumpy'];
var right = [];
var wrong = [];

var correctGuesses = 0;
var incorrectGuesses = 0;
var response = [];
var html

function print(message) {
  var outputDiv = document.getElementById ('output')
  outputDiv.innerHTML = message;
}

function getAnswer () {
  response = prompt('What are the names of the Seven Dwarves?');
  response = response.toLowerCase ();
  if ((answers.indexOf (response) > -1) && (right.indexOf (response) === -1)) {
    correctGuesses += 1;
   right.push (response);
  } else {
   wrong.push (response);
    incorrectGuesses += 1;
  }} 

function printList (arr) {
  var listHTML = '<ol>';
   for ( var i = 0; i < arr.length; i += 1) {
    listHTML += '<li>' + arr[i] + '</li>';
  }
  listHTML += '</ol>';
  return listHTML;}

for (var i = 0; i < answers.length; i += 1) {
  getAnswer ()
}

html = 'You got ' + correctGuesses + ' correct! Well Done!'
html += '<h2>These are the guesses you got right:-</h2>';
html += printList (right);
html += '<h2>The ' + incorrectGuesses + ' you got wrong was/were:-</h2>';
html += printList (wrong);
html += '<h2>Did you guess the same one twice??</h2>';
print (html);

3 Answers

Well, it looks beautiful and appeared to work seamlessly. Looks like great work to me :D

Thanks John!

I have a question. I'm not great with JS. This line:

  • if ((answers.indexOf (response) > -1) && (right.indexOf (response) === -1))
  • I get that your checking the index of answers.
  • there's a space and then (response)
  • how does that syntax work?
  • what does that space and then response in the parenthesis do?

Sorry John, my explanation for this may seem a little amateur but still very new to this...

The spaces I tend to add in to make sure I can read it properly, just helps me keep it clean. So the way that I was hoping the line works is that the response we get from the prompt is put in to the conditional statement to see if it is included within the answers array (the seven names). Then I also wanted to make sure that the response hadn't already been accepted, so to make sure you don't get 7 out of 7 for just repeating the same correct name.

Hope that answers your question, cheers for the feedback too!

Well that's very cool. I would not call that amateurish at all. Sounds like you have a good grasp of the concepts. Is that something that was taught? or did you just figure it would work. Because it certainly seems to work very well.

Searching the arrays and conditional statements is Treehouse taught, but the use of searching both arrays to make sure both conditions are met before a correct answer was accepted was just me experimenting really. Quite pleased it worked! :-)