Next Steps: Asked Players for New Choice - my solution without loops. Basically a single manual loop


#1

Here is my solution for the item. I tried many things with odd behaviors, but it seemed the best way to handle this was to copy the all the code and paste it underneath the original code and wrap it in a function that gets called if the first bit of code gets a Tie result.

var redo = false;
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) {
        if(choice1 === choice2) {
            redo = true;
            return "you tied"
        }
        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"
            }
        }
        else if(choice1 === "scissors") {
            if(choice2 === "rock") {
                return "rock wins"
            }
            else {
                return "scissors win"
            }
        }
    };
    
    console.log("User: " + userChoice);
    console.log("Computer: " + computerChoice);
    
    compare(userChoice, computerChoice);
    
if(redo === true) {    

var userChoice = prompt("Tied Pick again: 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) {
        if(choice1 === choice2) {
            
            return "you tied"
        }
        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"
            }
        }
        else if(choice1 === "scissors") {
            if(choice2 === "rock") {
                return "rock wins"
            }
            else {
                return "scissors win"
            }
        }
    };
    
    console.log("User Second Choice: " + userChoice);
    console.log("Computer Second Choice: " + computerChoice);
    
    compare(userChoice, computerChoice);
}

#2

This seems against the no repeat, any ideas how to do it in a more correct/efficient manner?


#3

Loops are the most efficient way. You can use the same code over and over again. Saves a lot of time and space.


#4

The least thing you could improve is to get rid of the second declaration of your compare function just call it another time. I mean that is what functions are for calling them multiple times instead of rewriting the code yourself.