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
Alexandra Wakefield
1,866 PointsMy Code for the Conditional Challenge
var questions = [
a = prompt("Question 1 of 5: What is the name of the first of the three thoracic segments of an insect?"),
b = prompt("Question 2 of 5: What is the name of tongue-like structure that molluscs use to scrape up food?"),
c = prompt("Question 3 of 5: What is the fibrous protein that makes up many sponge frameworks?"),
d = prompt("Question 4 of 5: What is the pincher-like appendage that some crustaceans and arachnids have?"),
e = prompt("Question 5 of 5: What is the name of the beak of a bivalve?")
];
//prothorax,radula,spongin,chela,umbo
var answers = [
a = questions[0].toUpperCase(),
b = questions[1].toUpperCase(),
c = questions[2].toUpperCase(),
d = questions[3].toUpperCase(),
e = questions[4].toUpperCase()
];
/*
Rank the Player
All Correct = Gold Crown
3-4 Correct = Silver Crown
1-2 Correct = Bronze Crown
None Correct = No Crown
*/
var crown = [
gold =
answers[0] === "PROTHORAX" &&
answers[1] === "RADULA" &&
answers[2] === "SPONGIN" &&
answers[3] === "CHELA" &&
answers[4] === "UMBO",
silver =
answers[0] === "PROTHORAX" &&
answers[1] === "RADULA" &&
answers[2] === "SPONGIN" &&
answers[3] === "CHELA"
||
answers[0] === "PROTHORAX" &&
answers[1] === "RADULA" &&
answers[2] === "SPONGIN" &&
answers[4] === "UMBO"
||
answers[0] === "PROTHORAX" &&
answers[1] === "RADULA" &&
answers[3] === "CHELA" &&
answers[4] === "UMBO"
||
answers[0] === "PROTHORAX" &&
answers[2] === "SPONGIN" &&
answers[3] === "CHELA" &&
answers[4] === "UMBO"
||
answers[1] === "RADULA" &&
answers[2] === "SPONGIN" &&
answers[3] === "CHELA" &&
answers[4] === "UMBO"
||
answers[2] === "SPONGIN" &&
answers[3] === "CHELA" &&
answers[4] === "UMBO"
||
answers[0] === "PROTHORAX" &&
answers[3] === "CHELA" &&
answers[4] === "UMBO"
||
answers[0] === "PROTHORAX" &&
answers[1] === "RADULA" &&
answers[4] === "UMBO"
||
answers[0] === "PROTHORAX" &&
answers[1] === "RADULA" &&
answers[2] === "SPONGIN"
||
answers[1] === "RADULA" &&
answers[2] === "SPONGIN" &&
answers[3] === "CHELA",
bronze =
answers[3] === "CHELA" &&
answers[4] === "UMBO"
||
answers[0] === "PROTHORAX" &&
answers[4] === "UMBO"
||
answers[0] === "PROTHORAX" &&
answers[1] === "RADULA"
||
answers[1] === "RADULA" &&
answers[2] === "SPONGIN"
||
answers[2] === "SPONGIN" &&
answers[3] === "CHELA"
||
answers[0] === "PROTHORAX"
||
answers[1] === "RADULA"
||
answers[2] === "SPONGIN"
||
answers[3] === "CHELA"
||
answers[4] === "UMBO"
];
if(crown[0] === true) {
alert("Wow, you got all five right! You really know your stuff! You earned the Gold Crown!");
} else if(crown[1] === true) {
alert("You're almost there, you got most of them right! You earned the Silver Crown!");
} else if (crown[2] === true) {
alert("You still have a lot to learn, but you got some right! You earned the Bronze Crown!");
} else {
alert("Sorry, you got all of them wrong. No crown for you.");
}
There is definitely a lot of repetition going on. I wanted to change the answers into a number using parseInt, but I'm not too sure how. I'll definitely be looking around the other discussions on this post to see if anyone else did actually change them into numbers.
Alexandra Wakefield
1,866 PointsThat's exactly what I was missing, Miles, a correctCount variable with if statements inside of it that changes the correct answers into a number I can use instead of filling my code with a repeat of the answer. Thank you.
I updated my code to do just that!
var questions = [
a = prompt("Question 1 of 5: What is the name of the first of the three thoracic segments of an insect?"),
b = prompt("Question 2 of 5: What is the name of tongue-like structure that molluscs use to scrape up food?"),
c = prompt("Question 3 of 5: What is the fibrous protein that makes up many sponge frameworks?"),
d = prompt("Question 4 of 5: What is the pincher-like appendage that some crustaceans and arachnids have?"),
e = prompt("Question 5 of 5: What is the name of the beak of a bivalve?")
];
//prothorax,radula,spongin,chela,umbo
var answers = [
a = questions[0].toLowerCase() === "prothorax",
b = questions[1].toLowerCase() === "radula",
c = questions[2].toLowerCase() === "spongin",
d = questions[3].toLowerCase() === "chela",
e = questions[4].toLowerCase() === "umbo"
];
var correct = 0;
if(answers[0]) {
correct += 1;
}
if(answers[1]) {
correct += 1;
}
if(answers[2]) {
correct += 1;
}
if(answers[3]) {
correct += 1;
}
if(answers[4]) {
correct += 1;
}
/*
Rank the Player
All Correct = Gold Crown
3-4 Correct = Silver Crown
1-2 Correct = Bronze Crown
None Correct = No Crown
*/
if(correct === 5) {
alert("Wow, you got all five right! You really know your stuff! You earned the Gold Crown!");
} else if(correct === 4 || correct === 3) {
alert("You're almost there, you got most of them right! You earned the Silver Crown!");
} else if (correct === 2 || correct === 1) {
alert("You still have a lot to learn, but you got some right! You earned the Bronze Crown!");
} else {
alert("Sorry, you got all of them wrong. No crown for you.");
}
Now, this is definitely easier on the eyes and to understand. Also, it just makes more sense.
Miles Aylward
13,225 PointsGlad I could help!!
Alexandra Wakefield
1,866 PointsAll right, here's my finial edit. In this one I took out the unnecessary answers variable and combined that into the correct if statements. I also added a sentence to the alert boxes that tells the player the number of answers they got correct.
var questions = [
a = prompt("Question 1 of 5: What is the name of the first of the three thoracic segments of an insect?"),
b = prompt("Question 2 of 5: What is the name of tongue-like structure that molluscs use to scrape up food?"),
c = prompt("Question 3 of 5: What is the fibrous protein that makes up many sponge frameworks?"),
d = prompt("Question 4 of 5: What is the pincher-like appendage that some crustaceans and arachnids have?"),
e = prompt("Question 5 of 5: What is the name of the beak of a bivalve?")
];
//prothorax,radula,spongin,chela,umbo
var correct = 0;
if(questions[0].toLowerCase() === "prothorax") {
correct += 1;
}
if(questions[1].toLowerCase() === "radula") {
correct += 1;
}
if(questions[2].toLowerCase() === "spongin") {
correct += 1;
}
if(questions[3].toLowerCase() === "chela") {
correct += 1;
}
if(questions[4].toLowerCase() === "umbo") {
correct += 1;
}
/*
Rank the Player
All Correct = Gold Crown
3-4 Correct = Silver Crown
1-2 Correct = Bronze Crown
None Correct = No Crown
*/
if(correct === 5) {
alert(correct + " out of 5: Wow, you got all five right! You really know your stuff! You earned the Gold Crown!");
} else if(correct === 4 || correct === 3) {
alert(correct + " out of 5: You're almost there, you got most of them right! You earned the Silver Crown!");
} else if (correct === 2 || correct === 1) {
alert(correct + " out of 5: You still have a lot to learn, but you got some right! You earned the Bronze Crown!");
} else {
alert(correct + " out of 5: Sorry, you got all of them wrong. No crown for you.");
}
1 Answer
Miles Aylward
13,225 PointsIf you don't mind besting my answer trying to get some brownie points as of late! Thank you!
It's been a while but the way I went about this challenge was directly after each prompt to test if the answer was correct or not, simple if else statements, and use a variable 'correctCount' to keep track of the total number of correct answers. Then at the very end all you have to do is test what that number is equal to ie 1-5 and award the appropriate crown.
Miles Aylward
13,225 PointsMiles Aylward
13,225 PointsIt's been a while but the way I went about this challenge was directly after each prompt to test if the answer was correct or not, simple if else statements, and use a variable 'correctCount' to keep track of the total number of correct answers. Then at the very end all you have to do is test what that number is equal to ie 1-5 and award the appropriate crown.