How can i assign userChoice to choice1, and assign computerChoice to choice2


#1

how can i assign userChoice to choice1, and assign computerChoice to choice2?

Is it simply replace all the choice1 with userChoice, and all the choice2 with computerChoice?

i tried it, but it do not work!


#2

via the parameters:
your function should look like this:

var compare = function(choice1,choice2){....

so choice1 and choice2 should be the parameters and therefore the variables you should use inside the function. Now by calling the compare function you pass values (arguments) to these variables e.g.

compare(userChoice, computerChoice)

would result in:

choice1 = userChoice; // 1st argument to 1st parameter
choice2 = computerChoice; //2nd argument to 2nd parameter

#3

im sorry i still do not understand ,do you mean i should write choice1=userChoice choice2=computerChoice?
i tried this ,but it still do not work


#4

@han1070 nah man. i just ran into the same problem. leave your codes as it is;

function(choice1, choice2) { ...}

and also the

if (choice1 === choice2) {...}

just after all that (including un-commenting your top part of codes), include this line (which calls back the compare function, now comparing userChoice and computerChoice) ;

compare(userChoice, computerChoice);

That way, you're inserting userChoice into choice1 and vice versa. Hope this helps!


#5

I got it ,Thank you very much


#6

/*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!";
}

}


#7

A post was split to a new topic: Placeholder: ExerciseNumber ErrorMessage


#8

Would anyone please help me what is the problem of my following code::sweat:

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 $";
}
} ;

compare(userChoice,computerChoice);


#10

If I were to switch the positions of computerChoice and userChoice, then would choice1=computerChoice and choice2=userChoice?

do the numbers matter? could I have put in:

var compare = function(toy, blah)

would it read toy as userChoice and blah as computerChoice?


#11

please you post the correct code of this


#12

Hi there

this really did my head in for a bit.
So Choice1 and Choice2 inside the function take on the userchoice and computerchoice parameter?
Really weird.. Because for me i would much rather, (like above people mentioned)

use choice1 and choice2 in both the function's parameter braces (#,#) and in the functions if statements. Why change it to another name (confusing)
I can understand how you guys feel the need to have a choice1=userchoice and choice2=computerchoice....

oohhh my brain.
cause in the next exercise, they did exactly as i said above...

var input = prompt("Enter input value");
var controlVal = input / 2 + 3;
var multiplier = function(number, phase) {

var val = number * controlVal + phase;

console.log(val);

};

Taking that example and apply the choice1 and choice2?

var input = prompt("Enter input value");
var controlVal = input / 2 + 3;
var multiplier = function(number, phase) {

var val = choice1 * controlVal + choice2;

console.log(val);

};

This would work yeah????? cause essentially choice1 will = the first parameter(number) and Choice2 will = the second parameter (phase)