These are chat archives for MontCode/GeneralChat

30th
Dec 2016
Jonathan Herman
@lantech19446
Dec 30 2016 01:12
@SimplyPhy or @davidjcastner are you around tonight?
Eric Phy
@SimplyPhy
Dec 30 2016 01:21
Out at the moment; what's up?
Jonathan Herman
@lantech19446
Dec 30 2016 01:21
it's nothing pressing, looking for some help with JS but it can wait
David Castner
@davidjcastner
Dec 30 2016 15:29
@lantech19446 I'm available today
Jonathan Herman
@lantech19446
Dec 30 2016 15:31
I'm coming to the end of the second section of codeacademy's javascript course and they have us build a rock, paper, scissors game. everything seems to work except it doesn't pass the information from the global variables to the function
David Castner
@davidjcastner
Dec 30 2016 15:34
okay, do you have a link to the code or can post an example?
Jonathan Herman
@lantech19446
Dec 30 2016 15:36
I can post the whole thing, I've added console.logs for both global variables so I know they're storing correctly
David Castner
@davidjcastner
Dec 30 2016 15:36
okay sounds good
Jonathan Herman
@lantech19446
Dec 30 2016 15:37
var userChoice = prompt("Do you choose rock, paper or scissors?"); console.log(userChoice); var computerChoice = Math.random(); if (computerChoice < 0.34) { computerChoice = "rock"; } else if(computerChoice <= 0.67) { computerChoice = "paper"; } else { computerChoice = "scissors"; } console.log(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); }
David Castner
@davidjcastner
Dec 30 2016 15:40
your statement compare(userChoice, computerChoice); is inside of your function compare is that intentional?
Jonathan Herman
@lantech19446
Dec 30 2016 15:43
no, I guess I didn't place much thought into it since all the variables we used throughout the lessons were local the calls came from inside the function
David Castner
@davidjcastner
Dec 30 2016 15:43
ah okay that's your issue then, Your function compare is never being called
one sec
Jonathan Herman
@lantech19446
Dec 30 2016 15:44
I just moved it outside but I still don't get a return for who won
David Castner
@davidjcastner
Dec 30 2016 15:45
it returns a string about who won, where do you need to input or display the result
Jonathan Herman
@lantech19446
Dec 30 2016 15:46
I'm not really sure where to expect it because it was done mostly in their online environment. I guess I could change out the returns for console.logs and have it print to the console
David Castner
@davidjcastner
Dec 30 2016 15:47
ok
Jonathan Herman
@lantech19446
Dec 30 2016 15:47
is there a way to do find and replace in atom?
David Castner
@davidjcastner
Dec 30 2016 15:48
ctrl+f
Eric Phy
@SimplyPhy
Dec 30 2016 15:49
@lantech19446 a good call to use 3 `s instead of just one to wrap multi-line code
David Castner
@davidjcastner
Dec 30 2016 15:49
// commenting this out for testing purposes as I don't have access to the function prompt
// var userChoice = prompt("Do you choose rock, paper or scissors?");
var userChoice = "rock"; // for testing

console.log(userChoice); // debugging

var computerChoice = Math.random();
if (computerChoice < 0.34) {
    computerChoice = "rock";
} else if (computerChoice <= 0.67) {
    computerChoice = "paper";
} else {
    computerChoice = "scissors";
}

console.log(computerChoice); // debugging

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";
        }
    }

    // where your compare statement was
    // compare(userChoice, computerChoice);
}

// what happens when you place the function call outside of the function
// running the function but because it returns a string, it won't do anything
// compare(userChoice, computerChoice);

// instead storing the result in a variable, which can be used however necesssary
var result = compare(userChoice, computerChoice);
console.log(result); // outputs the result to console
Eric Phy
@SimplyPhy
Dec 30 2016 15:51
missing semi-colon after ”scissors wins” and after the function declaration
David Castner
@davidjcastner
Dec 30 2016 15:51
@SimplyPhy lol
sometimes I love javascript
Eric Phy
@SimplyPhy
Dec 30 2016 15:53
var compare = function(…) {…}; requires semicolon, whereas
function compare(…) {…} doesn’t, just fyi @lantech19446
yeah lovely stuffs @davidjcastner :D
David Castner
@davidjcastner
Dec 30 2016 15:54
@SimplyPhy are you using a linter?
Eric Phy
@SimplyPhy
Dec 30 2016 15:54
yeah, as a gulp process
my browser sync on save lints for me
Jonathan Herman
@lantech19446
Dec 30 2016 15:55
That's actually a pretty good idea
Eric Phy
@SimplyPhy
Dec 30 2016 15:55
right now the lint setups i have on sublime are all screwed up because they can’t read angular/sass properly, so i keep them off
i get around to fixing them someday xD
David Castner
@davidjcastner
Dec 30 2016 15:55

i get around to fixing them someday xD

story of my life

Eric Phy
@SimplyPhy
Dec 30 2016 15:56
ha yup :sigh:
Jonathan Herman
@lantech19446
Dec 30 2016 15:57
yea the function didn't have the semicolon I had one when I declared userChoice but I thought i needed it because I set it equal to a prompt instead of a function
Eric Phy
@SimplyPhy
Dec 30 2016 15:58
though in other news, this week began real creative work for me at the new job, which is awesome. the prior month had been design architecture rewrites, which was thousands of lines of code with very little imagination outside of some neat little flexbox re-orientations going from desktop to mobile
David Castner
@davidjcastner
Dec 30 2016 16:00
nice! what are you working on?
Eric Phy
@SimplyPhy
Dec 30 2016 16:02
actually that’s not entirely true, there was imagination, just not much in terms of UI/design
David Castner
@davidjcastner
Dec 30 2016 16:03
lol
Eric Phy
@SimplyPhy
Dec 30 2016 16:03
the code architecture itself was were the imagination came in
Jonathan Herman
@lantech19446
Dec 30 2016 16:05
now for some reason it's not logging anything lol
David Castner
@davidjcastner
Dec 30 2016 16:05
where are you running it?
Jonathan Herman
@lantech19446
Dec 30 2016 16:06
nm it was just firefox being a bitch
it's working on chromes console
like the entire thing actually works on chromes console
David Castner
@davidjcastner
Dec 30 2016 16:07
chrome has an awesome dev tool, although I can't compare to other as I haven't tried them
Jonathan Herman
@lantech19446
Dec 30 2016 16:08
Ty both for your help. Codeacademy never made a point of call placement there was a short lesson about scope of variables but I didn't realize it changed calls too
David Castner
@davidjcastner
Dec 30 2016 16:09
yea when you write a function, it is only declaring what the function does but not running it
Jonathan Herman
@lantech19446
Dec 30 2016 16:11
that part I understood I just didn't realize you couldn't call global variables into a function from within it
David Castner
@davidjcastner
Dec 30 2016 16:12
you can
Jonathan Herman
@lantech19446
Dec 30 2016 16:14
that seemed to be the issue with the rock scissor papers
David Castner
@davidjcastner
Dec 30 2016 16:20
var theScope = "outerScope";

var functionOne = function() {
    console.log("running function one:");
    // access to the variable theScope(gobal version)
    console.log(theScope);
    console.log(""); // spacer
};

var functionTwo = function() {
    console.log("running function two:");
    // the issue with scope comes when you declare a variable inside with the same name
    // because the function reserves the name theScope, it is a diffent object than the global version
    // therefore theScope is undefined
    console.log(theScope); // not the global version
    var theScope = "innerScope";
    console.log(theScope);
    console.log(""); // spacer
};

functionOne();
functionTwo();

// functionTwo doesn't change the global version of theScope, so it is still "outerScope"
functionOne();
Jonathan Herman
@lantech19446
Dec 30 2016 16:25
so even though you hadn't declared theScope inside the function yet at the point that you logged it it still would use the local version inside the function? and am I understanding this correctly that if I had used userChoice and computerChoice the entire way through the program I could have called them at the end to display results from within the function and it wouldn't have mattered
David Castner
@davidjcastner
Dec 30 2016 16:27
correct, but the function would still need to called and the results outputted to the console(which could of been done inside the functions compare). but using global variables in a such a manner is a bad practice
Jonathan Herman
@lantech19446
Dec 30 2016 16:29
ok got it now one other thing I'm kind of lost on, whenever we used return in the class it spit something out on their console and I just assumed it worked that way in the real world but apparently it doesn't. What is the real use of return?
David Castner
@davidjcastner
Dec 30 2016 16:33
so return does just that is returns a value of a function, so it can be used later on. Let's say you wanted to keep track of the how many wins each player had, then you might return the name of the player from your function (rather than a statement about who won). And then you can call your function multiple times and add a name to some sort of results tracker.
basically it's so than you can use the results of your function later on
Jonathan Herman
@lantech19446
Dec 30 2016 16:35
o ok cool that explains why nothing was showing up on their screen but it still said the code was right even though earlier they kept displaying the returns
David Castner
@davidjcastner
Dec 30 2016 16:35
and here's some more javascript fun facts, but NEVER do this:
var theScope = "outerScope";

var functionThree = function() {
    console.log("running function three:");
    // because of the way the javascript compiler works, you can even set variables before they are declared
    // further down I have a statement `var theScope;`
    // so the function reserves the name theScope before running
    // therefore `theScope` is not referring to the global version
    console.log(theScope); // not the global version, currently undefined
    theScope = "wtf!?!!?!";
    console.log(theScope);
    var theScope; // actually declaring the variable used above (say whaaaaaaa?)
    console.log(theScope); // still "wtf!?!!?!"
    console.log(""); // spacer
    // mind blown
};

functionThree();
// global version of `theScope` is still "outerScope"
console.log(theScope);
Jonathan Herman
@lantech19446
Dec 30 2016 16:39
lol I was confused about that when you wrote it out that way above
David Castner
@davidjcastner
Dec 30 2016 16:42
yea basically the compiler checks the whole function for declarations before running the code
Jonathan Herman
@lantech19446
Dec 30 2016 16:46
just figured out that I could actually do alert (compare(userChoice, computerChoice)); at the end and get closer to what I expected would happen
I still don't know that I could write any of this on my own but I feel like I'm understanding the stuff more than I was
David Castner
@davidjcastner
Dec 30 2016 16:52
that's all that matters, it will take time
Jonathan Herman
@lantech19446
Dec 30 2016 17:09
ok one more question, say I write a function somewhere else in the program and I want to rerun it later like the whole shebang any of the prompts alerts etc not just one aspect of it how would I invoke it
Jonathan Herman
@lantech19446
Dec 30 2016 17:16
actually I mistated that, it's not a function I want to rerun it's the userChoice variable I want to rerun
Eric Phy
@SimplyPhy
Dec 30 2016 17:19
you don’t rerun variables, but you can call the variable or reassign it anytime, so long as its available in the scope where you attempt to call/reassign/‘rerun'
Jonathan Herman
@lantech19446
Dec 30 2016 17:25
so I'm creating an if else scenario where if the user selects something that isn't an option it will come up with a popup that says invalid choice that much works but I then want it to take them back to the prompt for making a choice which is tied in directly to the userChoice variable and the proceeding if else statements so I'm not sure beyond else { alert(invalid choice!); } how to make that happen and I don't really have the terminology down well enough to find anything useful on google or SO
Eric Phy
@SimplyPhy
Dec 30 2016 18:58
maybe just a straight return; ?
Jonathan Herman
@lantech19446
Dec 30 2016 19:11
I need a loop but I'm not clear on how to do it yet so I kept userChoice as a global variable set it equal to "null" and created a function that encompasses the prompt and subsequent if else then calls the function and sets it equal to userChoice the problem I run into is if the user inputs an invalid choice I leave userChoice = "null" and want to use an if else that says if userChoice is null rerun the function that prompts for their choice but it keeps telling me I have a } where I don't need it and I definitely need it there so I'm not sure where I messed up yet. right now i have to run to the bank though. Maybe when I get back I can put it on github so you can see it more easily
Eric Phy
@SimplyPhy
Dec 30 2016 19:13
you can probably just post it here inside three `s too
depending on the line count, i guess
also, if you tells you there’s an extra }, there probably is an extra } :D
but maybe not; we’ll see
Jonathan Herman
@lantech19446
Dec 30 2016 19:55
var userChoice = "null";
var choice = function(choice){
    prompt("Do you choose rock, paper or scissors?");
if (choice === "rock") {
    userChoice = "rock";
}
    else if (choice === "paper") {
        userChoice = "paper";
    }
    else if (choice === "scissors") {
        userChoice = "scissors";
    }
    else {
            alert("invalid choice!");
            userChoice = "null";
        }
    }
choice (userChoice);
if (userChoice === "null") {
    function choice()
}
else {
    function compare()
}

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 "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";
                }
            }
        }
                alert (compare(userChoice, computerChoice));