Functions using Choice Values don't seem to work in freemode


#1


https://www.codecademy.com/courses/javascript-beginner-en-NhsaT/2/5?curriculum_id=506324b3a7dffd00020bf661


ERROR I GET IS Reference Error Choice1 is not defined. This is part of my ongoing inability to get this.

//My Example Program
//First make program function to divide a number by 3
//Make Rock Paper Scissors Game
//Make a Loop count down from 100 in increments of 5
//hahah OK
//Divide by 3 Function
var divideByThree = function (numberToDivide) {
    var result = numberToDivide / 3;
    console.log(result);
}
var userInput = prompt("Enter a number to Divide by 3");
divideByThree(userInput);
//YAY
//MY ROCK PAPER SCISSORS GAME
//Computer Pick
var computerChoice = Math.random(2);
console.log(computerChoice);
if(computerChoice <=0.33) computerChoice = "Rock";
if(computerChoice >=0.66) computerChoice = "Paper";
else computerChoice = "Scissors";
//USER Choice
var userChoice = prompt ("Rock Paper or Scissors? ");
var compare = function (computerChoice,userChoice) {
    computerChoice = choice1;
    userChoice = choice2;
    if(choice1 === choice2) console.log ("It's a Tie");
    if(choice1 === "rock") {
        if(choice2 === "paper") console.log ("You win, Paper Covers Rock"); else console.log ("You lose!");
        }
    }
    if(Choice1 === "paper") {
        if(choice2 === "scissors") console.log ("You win scissors cut paper"); else console.log ("You lose!");
        }

    if(choice1 === "scissors") {
        if(choice2 === "rock") console.log ("You win, Rock blunts Scissors"); else console.log ("You lose!");
    }
 compare(computerChoice,userChoice);


#2

// function declaration with parameter computerChoice and userChoice
var compare = function (computerChoice,userChoice) {
    /* function content */
    console.log(computerChoice)
    console.log(userChoice)
}
// function call with arguments computerChoice and userChoice
compare(computerChoice,userChoice);

Please read the comments in code above, i am going to use terms from from above example

Why did you give your function parameters of computerChoice and userChoice? the parameters should be choice1 and choice2, the parameters serve as placeholder until you call the function, then you supply arguments. The values of the arguments get copied into the function parameters.


#3

Hi there,
I don't even know how to explain my confusion anymore.
I'm just going to assume from now on if i make a function

var subtract = function (num1,num2) {
//Make sure not negative
if(choice1 - choice2 = < 0){
console.log("Result is Negitave");
}
}
subtract(5,3);

as above to me my if statement is WRONG because its using choice1 and choice2 which were never defined (but apparently doesn't matter cause they inherit the parameters from function anyway). My belief is that I should be using num1 and num2 respectively like this --> . if(num1- num2 = < 0) - just like in the Declared function (num1, num2){}
But from my questionings and playing around the if(choice works like num1 would but only codeacedemy GUI for the specific lesson and not when just playing with code end of lesson.

OMG I hope that makes sense. :slight_smile:
lol I don't want to make a big deal of this, i can see you guys pulling your hair out trying to explain to a newb.
I will try it in Visual Studio or Notepad++ or something see how i go


#4

A signal to step back. If anything, a walk, and then a review of everything you've learned to this point. Refresh yourself. That will assuredly relieve some of the confusion. We all learn this way. Be calm and prepared to tear down some of your code.

Notice in the above your parameters are num1 and num2, yet the variables you query are choice1 and choice2? That is a problem.


#5

Is this tried and true logic? I would question it, and suggest you do the same.


#6

A word of caution... Best not stray too far from the lesson expectations. This is not the best time for self-expression. Make sure your patterns conform to expected syntax

If you are trying to make this a project of your own, then we will have to move it to the Corner Bar. Otherwise, conform.


#7

That's exactly what i thought. Thanks for clarification


#9

lol. Conform. like the slave i am :stuck_out_tongue:


#10

Ok, I am questioning it.

if(computerChoice <=0.33) computerChoice = "Rock"; // [range 0 - 0.33]
else if(computerChoice >=0.66) computerChoice = "Paper"; // [range 0.66 - 1]
else computerChoice = "Scissors"; // [other range between these values can only be between]

are you expecting me to use return statements? or maybe organised the if statement like below;

if(computerChoice <=0.33) computerChoice = "Rock";
else if(computerChoice <=0.66) computerChoice = "Paper";
else computerChoice = "Scissors";

Like a sort of process of elimination, assuming the else wont kick in if an If statement has already been followed?
are you referring to non used of {} for the If Statements? Many different ways to achieve the same thing, but of course i want to know the best way.

Thank you very much for your help mtf. Feel free to move this post if it isn't relevant to section.


#11

In your earlier example,

if ...
if ...
else ...

If choice becomes "rock", the second if is bypassed, but not the else, so choice becomes "scissors". That's the flaw in the logic as written.

Your second example uses if.. else if... else, that corrects that flaw.

The use of structure (code blocks) is the expected norm in most of these lessons...

if ( ... ) {

}
else if ( ... ) {

} else {

}

Note that your first example, even if written with correct logic would result in the wrong values being assigned.

rock, scissors, paper

instead of

rock, paper, scissors

?

We are not actually dealing with range, but interval which are very close to being the same thing. A range is seen as a sequence from a to b, where an iterval is everything between a and b. Small difference, and a minor one of semantics, mostly.

(0, 1/3], (1/3, 2/3], (2/3, 1]

Which is expressed in the inequalities,

{ x |   0 <= x < 1/3; x is Real }    // rock
{ x | 1/3 <= x < 2/3; x is Real }    // paper
{ x | 2/3 <= x < 1;   x is Real }    // scissors

#12

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