Next Steps.. Please help!


#1

Hello there! :slight_smile:

Newbie here and I wanted to learn more and more about JavaScript so I challenge myself and I want to answer this: What if a user makes an inappropriate choice like 'dog'? How can we extend the function to handle that?

Since I wanted the user to input "rock, paper, scissors" I decided to use this code below but sadly it's an unsuccessful attempt. Please help me learn this! :kissing_heart::kissing_heart:

NO ERROR MESSAGE its just doesn't workout right! :cry:


Replace this line with your code. 

var strictChoice = function(choices) {
    if(choices === "rock", "paper", "scissors")
        return compare;
     else {
        return "Please choose 'rock', 'paper', 'scissors'";
    }
};
strictChoice(userChoice)


#2

First of all this:

if(choices === "rock", "paper", "scissors")

will probably behave differently than what you might expect. In short: The comma operator chains two statements together that now count as 1 and have the value of the second value. For example:

var i=0;
if(i++,i==1){
    console.log("test");
}

The i++ is just executed but the i==1 is the statement that gives the value and thereby determines if the condition is true or false. So in your case you chain the following statements to be one condition:

statement 1: choices === "rock"
statement 2: "paper"
statement 3: "scissors"

where "scissors" is the real condition... Now when you google it you'll find that in JS non-empty strings are treated as if they meant true, so after all your if statement will always be true no matter what choices actually is. So instead of using the comma operator. Why not use && (logical AND) and || (logical OR) to chain your statements. But keep in mind that these statements are distinct so you need to go the long way of choices === "rock" || choices === "paper" || ...

Last but not least what do you intend by return compare?


#3

Hi @haxor789! Thank you so much for explaining this to me! I thought comma can separate choices(I'll google for this :slight_smile: ) However, just wondering if is it possible to make the choices into a simpler form? :grin:

I input return compare; so that the compare function I created will be the output of the correct choices. If this isn't correct how will I do this?? :persevere:

Thank you so much!!! :kiss:

BELOW IS THE CODE:

var userChoice = prompt("Do you choose rock, paper or scissors?");

//this will be the correct input for this game

var strictChoice = function(choices) {
    if(choices === "rock" , "paper" , "scissors") /*didn't change this yet*/
        return compare;
     else {
        return "Please choose 'rock', 'paper', 'scissors'";
    }
};
strictChoice(userChoice)

var computerChoice = Math.random();
if (computerChoice < 0.33) {
	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";
        }
    }
    else if(choice1 === "scissors") {
        if(choice2 === "rock") {
            return "rock wins";
        }
        else {
            return "scissors wins";
        }
    }
};
compare(userChoice, computerChoice)

#4

Well as said you could use the logical OR to do this:

if choice == "rock" OR choice == "paper" OR choice == "scissors"

or in JS:

if (choice == "rock" || choice == "paper" || choice == "scissors")

if you want to practice logic do it with AND (&&) instead :slight_smile:

And for the compare. Well in this case it might be better two move your computer and user choice after the function and call this compare(userChoice, computerChoice) inside of your pick function e.g.

compare function ...
var playGame = function(){
    computer picks
    user picks
    if correct
       return compare(user vs computer)
    else 
       sry bad input
}

PS: That is pseudo code it can not be run but is just to illustrate the thought process.


#5

hello @haxor789 Thank you so much for helping me! Though I can't do it for now but my hopes are still high and I will definitely come back in this exercises. :kissing_heart:


#6

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.