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 trialRishabh Ahuja
Full Stack JavaScript Techdegree Student 412 Pointswhy does this javascript code not function
// ask 5 questions
//track of correct answers and give feedback;
// if got all give them a rating like howdy , gold , silver
// lets start
// before declare variable score
// first ask questions
var score = 0;
function ask() {
q1 = confirm("Is javascript tough");
q2 = confirm("Is coding booring");
q3 = confirm("Is javascript related with java");
q4 = confirm("is jquery built on javascript ");
q5 = confirm("Is javascript userfriendly");
}
// check questions
function check() {
if (q1) {
score = 5;
}
if (q2) {
score++;
}
if (q3) {
score++;
}
if (q4) {
score++;
}
if (q5) {
score++
}
}
function rank () {
if (score == 5 ) {
document.write('good job and you got ' + score + ' correct');
}
else {
document.write("better luck next time");
}
}
ask();
rank();
iam trying to get the score feedback but it says better luck next time i understand that if statement will only executed once and rest will not be executed if first comes out to be true but i dont understand how will this work
5 Answers
Andrew Dale
Courses Plus Student 7,030 PointsHi Rishabh,
Mark is right when he says you don't call the check function. It is there but it isn't doing anything at the moment. I've come up with a solution for you which uses a lot of your original code but with minor tweaks:
// declare a variable with a score of 0 to begin
var score = 0;
/* create a function which asks the question.
I have changed your 'confirm' to a 'prompt' as this will require the user to enter their own response.
The function asks and checks the response in one hit.
It will only count towards the score if the user enters the correct answer ("yes" or "no").
In this instance, you can use == instead of === as the values
that are being checked are the same at both ends
(i.e. score is always a number and the questions are always strings),
but it is best practice to use === and !== at all times as they are more adaptable. */
function ask(){
q1 = prompt("Is javascript tough, yes or no");
if (q1 === "no".toLowerCase()){
score += 1;
}
q2 = prompt("Is coding booring, yes or no");
if (q2 === "no".toLowerCase()){
score += 1;
}
q3 = prompt("Is javascript related with java, yes or no");
if (q3 === "no".toLowerCase()){
score += 1;
}
q4 = prompt("is jquery built on javascript, yes or no ");
if (q4 === "yes".toLowerCase()){
score += 1;
}
q5 = prompt("Is javascript userfriendly, yes or no");
if (q5 === "yes".toLowerCase()){
score += 1;
}
}
// Now you want to give the user a rank.
function rank(){
if(score === 5){
document.write('good job and you got ' + score + ' correct');
} else {
document.write("better luck next time you only scored " + score);
}
}
/* Call the functions here. This is where you would
have called your "check" function in your code above.*/
ask();
rank();
You were really close with your code. Like I said above, just some minor tweaking. I hope this helps and makes sense.
Mark Hill
9,047 PointsYou haven't called the check function
Rishabh Ahuja
Full Stack JavaScript Techdegree Student 412 Pointsbut it will automatically be called as I have called the ask function as ask function requires check function to be executed
Mark Hill
9,047 PointsI'm not too sure what you mean, but at the moment it isn't being called. You could put check(); at the end of the ask function definition, or you could put check(); at the bottom. The if statements are fine as each of them will be checked separately. If you had if and else statements, then only one of them would be performed.
Rishabh Ahuja
Full Stack JavaScript Techdegree Student 412 Pointsok thanks can you suggest a better way of doing the same thing with more efficient code
Rishabh Ahuja
Full Stack JavaScript Techdegree Student 412 Pointsthank you very much