This forum is now read-only. Please use our new forums! Go to forums

0 points
Submitted by Ko0lk4t Jr
over 10 years

Just wanted someone with more experienced to check

Hi I have past this bit of the training however my answer is always a tie and wondered if anyone could shed light on why it keeps happening would be much appreciated

var userChoice = prompt("Do you choose rock, paper or scissors?");
var computerChoice = Math.random();
if (computerChoice < 0.34) {
    computerChoice = "rock";
} else if(computerChoice <= 0.67) {
    computerChoice = "paper";
} else {
    computerChoice = "scissors";
}
var compare = function (choice1, choice2) {
    compare = (choice1 === choice2);{
        return "The result is a tie";
    }
    if (choice1 === "rock") 
    {
    if (choice2 ==="scissors"){
        return "rock wins";
    } 
    else {
        return "paper wins";
        }
    }

};
var compare = function(choice1, choice2) {

if (choice1 === choice2) {
    return "The result is a tie."; {

    if (choice1 === "rock") {  // true so step in here
            if (choice2 === "scissors") // false so skip rest
                return "rock wins";
        } else {
            return "paper wins";
        }
}
    if (choice1 === "paper") { // false so skip to else
            if (choice2 === "rock")
                return "paper wins";
        } else {   // here is where you are getting returned from
            return "scissors wins";
    }
}

};
var compare = function (choice1, choice2) {
    if (choice1 === choice2){
        return "The result is a tie!"; {
          if (choice1 === "scissors") {
              if (choice2 === "rock")
              return "rock wins";
          } else {
              return "scissors win";
          }
          
        }
    }
};
compare (userChoice, computerChoice);

[Editted by Aden Fission: Fixed code formatting]

Answer 524bfd3babf821767800206b

0 votes

Permalink

  1. You have defined the function ‘compare’ thrice, which is unnecessary (and extremely likely to cause bugs). Get rid of the two excess function expressions. You can define all the necessary functionality within a single function.

Within the course, you are actually asked to make editions on the same function, not rewrite the function every time.

  1. I’ll focus on (what appears to me) the most logically written function (the second one). Also note that commenting code is useful not only for other developers, but also for yourself (especially when you are just starting out): Here’s a model of the function

  2. Use indentation liberally for yourself and curly brackets intelligently for your computer. Try to close a curly bracket as soon as you can (the code editor here automatically inserts curlies for you) and simply type your code within the curlies. Remember that anything not within a function’s curlies will NOT get executed when the function is called. Hopefully useful tut.

points
Submitted by Aden Fission
over 10 years

Answer 5250783bf10c60448f00a069

0 votes

Permalink

You can refer to this code,if you want.

snip

[Removed for providing uncommented/unexplained solution code. Please provide either hints or commented code to help in learning. Thanks! ~ Aden]

points
Submitted by Ankit Sharma
over 10 years

1 comments

Aden Fission over 10 years

Hey Ankit, nice to see your helpful attitude! :D Could you not give away the full answer next time, though? That really deters from the learning experience (and even if you do give away the full answer, comment it sufficiently to make it useful for learning). Thanks, and good luck!