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 Basics (Retired) Making Decisions with Conditional Statements The Conditional Challenge

daniel zaragoza
daniel zaragoza
1,843 Points

Is there way??

I'm always trying to learn, so is there a way to make the code below a simpler and make it look cleaner. Because i know there's always a shorter and easier way.

var answer =+ 0 ;
var question1 = prompt(' The song bullet is from what arist?');
var question2 = prompt(' The song date rape is by what artist');
var question3 = prompt(' The song behind blues eyes is by what artist? ');
var question4 = prompt(' The song welcome is by what artist? ');
var question5 = prompt( " The song the one is by what artist ");
if(question1.toUpperCase() === "HOLLYWOOD UNDEAD"){
   answer = 1;
}if(question2.toUpperCase() === "SUBLIME"){
  answer = 1;
}if(question3.toUpperCase() === "LIMP BIZKIT"){
  answer = 1;
}if(question4.toUpperCase() === "FORTMINOR" ){
  answer = 1;
}if(question5.toUpperCase() === "DUCE"){
  answer = 1;
}
if(answer === 5){
  alert(" you get gold crown");
}else if(answer < 4 ){
  alert(" You get silver crown");
}else if(answer < 2){
  alert(" you get bronze crown");
}else{
   alert(" Loser");
}

3 Answers

Tobias Helmrich
Tobias Helmrich
31,603 Points

Hey Daniel,

I'm sorry, it's not really a tip how to get the code cleaner and simpler but I noticed some flaws in your code.

The first flaw is when you check the answers, you're comparing the value to a band's name. So far it's correct. However when the condition is true you're always setting the variable answer to 1. This means you're always overwriting it with a value of one and so it can never become more than one. The part I'm talking about is this one:

if (question1.toUpperCase() === "HOLLYWOOD UNDEAD") {
    answer = 1;
}
if (question2.toUpperCase() === "SUBLIME") {
    answer = 1;
}
if (question3.toUpperCase() === "LIMP BIZKIT") {
    answer = 1;
}
if (question4.toUpperCase() === "FORTMINOR") {
    answer = 1;
}
if (question5.toUpperCase() === "DUCE") {
    answer = 1;
}

A way to fix this is the following:

if (question1.toUpperCase() === "HOLLYWOOD UNDEAD") {
    answer++;
}
if (question2.toUpperCase() === "SUBLIME") {
    answer++;
}
if (question3.toUpperCase() === "LIMP BIZKIT") {
    answer++;
}
if (question4.toUpperCase() === "FORTMINOR") {
    answer++;
}
if (question5.toUpperCase() === "DUCE") {
    answer++;
}

answer++ is a short way to write "answer = answer + 1" and it will increment the value each time a condition is true.

The second flaw is that you don't have conditions in your else if statement here:

else if (answer alert(" You get silver crown");
} else if (answer alert(" you get bronze crown");
}

You don't close the parentheses and you also don't open the curly braces. And there also is no condition except answer.

To make it work you could write it like this:

else if(answer < 5 && answer >= 3) {
    alert(" You get the silver crown");
} else if(answer < 3 && answer > 0) {
    alert("You get the bronze crown");
}

And about making the code simpler and cleaner: You're still in the JavaScript basics track so you might want to check out the JavaScript loops ans arrays course here on Treehouse after you're done with the basics. With loops and arrays you'll be able to write the code you posted much cleaner and shorter! :)

I hope that helps!

Good luck!

This is how I would write the code. jsFiddle Demo

I hope this helps.