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

Possibly gather some feedback on my Conditional Challenge?

Just trying to gather some feedback on my code for the conditional challenge.

I tried using

if (conditional){
return counter
}

but that would just end my program. So this what I have so far.

// This is the counter
var counter = 0;

// This is Question 1
var qOne = parseInt(prompt('What is 5 - 2?'));

// If user's answer is correct, counter adds 1
if (qOne === 3){
  alert('That is correct.')
  counter++;
} else {
  alert ('That is incorrect')
  counter = counter;
}

// This is Question 2
var qTwo = parseInt(prompt('What is 20 - 5?'));

// If user's answer is correct, counter adds 1, otherwise counter doesn't change
if (qTwo === 15){
  alert('That is correct.')
  counter++;
} else {
  counter = counter;
}

// This is Question 3
var qThree = prompt('What is the capital of Washington State?');

// If user's answer is correct, counter adds 1, otherwise counter doesn't change
if (qThree === 'Olympia' || qThree === 'olympia'){
  alert('That is correct.')
  counter++;
} else {
  alert ('That is incorrect');
  counter = counter;
}

// This is Question 4
var qFour = prompt('Stop signs are what color in the US?');

// If user's answer is correct, counter adds 1, otherwise counter doesn't change
if (qFour === 'Red' || qFour === 'red'){
  alert('That is correct.')
  counter++;
} else {
  alert ('That is incorrect');
  counter = counter;
}

// This is Question 5
var qFive = parseInt(prompt('What is 5 * 10'));

// If user's answer is correct, counter adds 1, otherwise counter doesn't change
if (qFive === 50){
  alert('That is correct.')
  counter++;
} else {
  alert ('That is incorrect');
  counter = counter;
}

// Player rewards based on correct answers
if (counter === 5){
    document.write('You earned a gold crown. You scored ' + counter +' out of 5.');
  } else if (counter === 3 || counter === 4){
    document.write('You earned a silver crown. You scored ' + counter +' out of 5.');
  } else if (counter === 1 || counter === 2){
    document.write('You earned a bronze crown. You scored ' + counter +' out of 5.');
  } else {
    document.write("Apologies, you got all the questions wrong. No crown for you.");
}

1 Answer

Hey Nathaniel

Return is only used for functions to send information back to its caller. If you for example have a function bigger() which takes two arguments and returns the bigger number of those two and assign it to a variable, then you need to use return. And every time a return happens, it will stop the rest of the function and proceed with the rest of your program.

It would look something like this:

function bigger(a, b) {
  if (a > b) {
    return a;
  }
  else {
    return b;
  }
}

To get the bigger value and store it to a variable, then you will probably call it like this:

var biggerNum = bigger(3, 5); //in this case '5' will be returned and get stored in the variable biggerNum

I hope this helps you to understand about 'return'.

Happy coding! Egarat