JS Assignment Help
August 7th, 2016
The instructions for this module are as follows:
Inside the compare() function under the existing code, write another else if statement where the condition is choice1 === "paper".
.[/quote]
Inside this else if statement, write an if / else statement. If choice2 === "rock", return "paper wins". Else, return "scissors wins"
I’ve read through all the posts on their help forum, tried various solutions, and used a code tester to find errors. However I still can’t get this to run. Currently, the tester says there’s an error (that I can’t solve) on line 23 and line 30. Originally, I had an extra “if” before line 23 based on the hint they give on the site, but took it out based on what I was seeing from others. I’d appreciate any helpful input , thanks in advance
/*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";
}
}
else if (choice1 === "paper"){
if (choice2 === "rock")
return "paper wins";
}
else{
return "scissors wins";
}
}
afaik tripple === is redundant. = : set variable as xxx
== : check if variable is xxx, hence != : value different from xxx
=== : invalid?
the === actually works as it was from the previous assignment. If I take it out, it’ll mess up the required code further. It’s there to show “equals to” value between the 2 choices
Check the braces.
These are the changes I’ve made, which corrected a couple issues. However the tester still says I’m missing a semicolon on line 22, which I can’t figure out where. I’ve literally put it in every position and it’s still an error. The console log says “SyntaxError: missing before statement'”
I’m totally at a loss. I’ve been at this since this morning and it’s just frustrating the hell out of me.
/*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";
}
Else if (choice1 === "paper"){
if (choice2 === "rock")
return "paper wins";
else
return "scissors wins";
}
};
i’ve had a little go at it too with some online parsers and what i found was
1) they tend to want a space (or return) between an ) and opening {
2) one of them said === implied “strict use” and the required accompanying statement of “use strict” (WITH QUOTES) was missing.
3) your entire randomizing sequence is commented out /* and */
4) your using a variable “compare” that pulls a fuction (that doesn’t exist?) that uses 2 variables choice1 and choice2 that don’t exist either. As such your compare and if statements have nothing to work with.
Other than that, i haven’t got a clue about JS.
Yeah, everything you said makes sense. But I guess he’s set the assignment to only accept a certain code entry, which is really frustrating. The commented section was automatically done by the program for what reason, I don’t know. I got it to finally run using this code, but it still doesn’t feel right to me.
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 { // paper beats rock
return "paper wins";
}
} else if (choice1 === "paper") { if (choice2 === "rock") { return "paper wins";
} else { // Scissors beat paper
return "scissors win";
}
} else { //Choice1 is "scissors"
if (choice2 === "rock") { return "rock wins";
} else { // Scissors beat paper
return "scissors win";
}
}
};
compare (userChoice,computerChoice)