Escape Rope


#1

Hi All,

I am trying to do the Next Steps for building the Rock Paper Scissors game.
As you can see below I am adding a rope to the game; originally I wanted to add the escape rope mechanism as a new function instead of expanding the previous one. It doesn't work as seen below:

var userChoice = prompt("Do you choose rock, paper or scissors?");
var computerChoice = Math.random();
if (computerChoice < 0.26) {
	computerChoice = "rock";
} else if (computerChoice <= 0.50) {
	computerChoice = "paper";
} else if (computerChoice <= 0.75) {
	computerChoice = "scissors";
} else {
    computerChoice = "rope";
}
console.log("Player: " + userChoice);
console.log("Computer: " + computerChoice);

var checkRope = function(check1, check2) {
    if (check1+check2==="roperope") {
        return "Both players have escaped with a rope! *chirp, chirp*"
    } else if (check1 === "rope") {
        return "Player has escaped with a rope!"
    } else if (check2 === "rope") {
        return "Computer has escaped with a rope!"
    }
}

var compare = function(choice1, choice2) {
    checkRope(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";
        }
    } else if (choice1 === "scissors") {
        if (choice2 === "rock") {
            return "rock wins";
        } else {
            return "scissors wins"
        }
    } else {
        return "Oops! Please choose either rock, paper or scissors.";
    }
}

compare(userChoice, computerChoice)

However if I take the contents of the checkRope function and paste it in directly to the compare function it then works.

var userChoice = prompt("Do you choose rock, paper or scissors?");
var computerChoice = Math.random();
if (computerChoice < 0.26) {
	computerChoice = "rock";
} else if (computerChoice <= 0.50) {
	computerChoice = "paper";
} else if (computerChoice <= 0.75) {
	computerChoice = "scissors";
} else {
    computerChoice = "rope";
}
console.log("Player: " + userChoice);
console.log("Computer: " + computerChoice);

var checkRope = function(check1, check2) {
    if (check1+check2==="roperope") {
        return "Both players have escaped with a rope! *chirp, chirp*"
    } else if (check1 === "rope") {
        return "Player has escaped with a rope!"
    } else if (check2 === "rope") {
        return "Computer has escaped with a rope!"
    }
}

var compare = function(choice1, choice2) {
    if (choice1 === "rope", choice2 === "rope") {
        return "Both players have escaped with a rope! *chirp, chirp*"
    } else if (choice1 === "rope") {
        return "Player has escaped with a rope!"
    } else if (choice2 === "rope") {
        return "Computer has escaped with a rope!"
    };
    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";
        }
    } else if (choice1 === "scissors") {
        if (choice2 === "rock") {
            return "rock wins";
        } else {
            return "scissors wins"
        }
    } else {
        return "Oops! Please choose either rock, paper or scissors.";
    }
}

compare(userChoice, computerChoice)

Can we not use functions inside of functions? What is happening here? Why does it work one way but not the other?


#2

Well, it does work, the only problem you have is when you call a function inside a function, return doesn't get logged to the console.anyway, if you where to change this line:

checkRope(choice1, choice2);

to:

console.log(checkRope(choice1, choice2));

you will see it does work. But then your second function still continues, so you might want to consider to use one function instead of two


#3

That makes sense, thank you. Explaining the way return resolves cleared this up for me.