Can someone debug this pls im stressing out over it (its unfinished btw)


#1

var userChoice = prompt("Do you choose rock, paper or scissors, lizard, or spock?");
if (userChoice !== "rock" && "paper" && "scissors" && "lizard" && "spock"){
console.log("Not a valid answer!")
}
console.log("User: " + userChoice)
//this is the code for the display of the user choice and the choice itself
var computerChoice = Math.random();{
if (computerChoice < 0.20){
computerChoice = "rock";
}
else if(computerChoice <= 0.40){
computerChoice = "paper";
}
else if(computerChoice <= 0.60){
computerChoice = "scissors";
}
else if(computerChoice <= 0.80){
computerChoice = "lizard";
}
else if(computerChoice > 0.81){
computerChoice = "spock";
}
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 === "scissors"){
return ("scissors wins")
}
else{
return ("paper wins")
}
}
else if (choice1 === "scissors"){
if (choice2 === "paper"){
return ("scissors wins")
}
else{
return ("rock wins"){
}
}

}
var notATie = function (choice1, choice2){
if (userChoice === computerChoice){
console.log("TIEBREAKER!!!")
prompt("Do you choose rock, paper or scissors?")
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 === "scissors"){
return ("scissors wins")
}
else{
return ("paper wins")
}
}
else if (choice1 === "scissors"){
if (choice2 === "paper"){
return ("scissors wins")
}
else{
return ("rock wins")
}
}
}

compare(userChoice, computerChoice)
notATie(userChoice, computerChoice)


#2

It appears to me that you have 'unexpectedly' nested a block of else if statements beginning at the end of declaring the variable on line 7, and nesting all in-between 7 and line 24. Additionally, at line 50, is another unexpected set of curly brackets that contain nothing. After ridding that set, be sure to add a closing curly-bracket to close the function.

Finally, be sure that you also use proper syntax and use appropriate semi-colons when applicable*

I'm not sure if that is all problems (other forum-goers, please correct me if I'm wrong), but after applying these fixes, the script successfully executes within my browser.


#3

Thank you for your help, but as a beginner i do not know how to fix "nested" blocks. If you could please explain a little further i'd be very grateful. Thank you for help anyway.


#4

Firstly, sorry for my extremely delayed reply, but I was very occupied within these days. I feel replying is still necessary in-case another individual might find use of this information.

Sorry to confuse you, but aside from nesting being another term used within JavaScript, I am using the term to refer to your use of the {} symbols (curly brackets) at the end of line 7 following the declaration of computerChoice that holds together a block of code.
Use of these brackets especially after the variable is not expected, and what this is doing is taking everything within the brackets on line 7 - 24 and breaking all the code within it (I'm sure there are more proper terms for this kind of result, but I'm not that familiar with them). To fix this, all you have to do is find where the starting bracket and closing bracket of this block you are creating is, and get rid of them (line 7 and 24) - thus opening the block back up into it's proper state.

The same is happening on line 50, but along with these brackets not being expected, there is no data within them.