Rock,paper,scissors, bugging.. JavaScript


#1

Can anyone please tell me why is this allways returning "The result is a tie!" what ever the input... ??

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";
} console.log("Computer: " + computerChoice);

var compare = function(choice1,choice2) {
if (choice1 === choice2){
return "The result is a tie!";
}
else if (choice1 === "rock"){
if (choice2 === "scissors"){
return "rock wins";
}
else {
return "paper wins";
}
}
}
console.log(compare())


#2

because when you call the function:

console.log(compare())

you don't supply any arguments for the parameters


#3

okkk.... so how do i fix that? im pretty sure this is the way they instructed me to do it..


#4

you learned about parameters and arguments:

https://www.codecademy.com/en/courses/javascript-beginner-en-6LzGd/0/3?curriculum_id=506324b3a7dffd00020bf661

arguments is what you supply at function call to satisfy the parameters


#5

yes, but is't the Prompt satisfying the parameter in this one? can u please just post me the this code fixed... i'm sure i'l figure it out my self, i would be very grateful!


#6

your function has two parameters:

var compare = function(choice1,choice2)

so at function call you should supply two arguments:

console.log(compare(argument1, argument2))

the values of the argument get copied into the function parameter


#7

welll thank you very much friend..... that make's it much clearer...


#8

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.