5. How to get it to print the actual result


#1

5. Both choices are the same

I am not stuck on this one. I have gotten past it. This is the code I entered:

var compare = function(choice1, choice2) {
    choice1 === userChoice;
    computerChoice === choice2;
    if(choice1 === choice2) {
        return ("The result is a tie!");
    }
    else {
        return ("someone has won");
    }
};

console.log(compare)

My issue is that the result is just:
"computer:rock
[function]"

When I try and print the var compare it just gives me [function]. Why doesn't it give me the result? how can I get it to print the return. The return is obviously a string that is meant to be shown to the user yet no matter what i try, local or global, inside the if statement or out in all the different ways I can think of trying it.

return the value is suppose to give modify and give the programmer back the value, the value is a string that is impossible(for me anyway to print) so what is going on here?

I know I am going off the beaten path here and this isn't the task but it is bugging my happiness that I can't work out how to do this. I thought I was beginning to understand js but this just throws my thinking into a tailspin and I have no idea what this language can or can't do anymore so what I really want is some clarity and I will be extremely grateful to anyone that can give me that clarity in clear, unambiguous and technical terms.

Thanks and best regards
Ryan.


#2

call the function
like
console.log(compare());

without calling it you'll never get the result

and remember == or === not same

@rkdownie12gmail.com


#3

Hi Ryan,

Here is a three part explanation with example programs from the old CC forum that explains the difference between print and return. Here are alternatives if you find the first one confusing: [1, 2, 3].

The gist of it seems to be that

  1. return; must be used inside a function. It will not necessarily show you what it is.
  2. console.log(); is use mainly for debugging purposes.

Tangentially relevant is that sometimes you might see the return value in this class's console but not in another (explanation).


Back to your question, at this point in this lesson you are just creating the function; you haven't run it yet.

Looking at your program, can you tell me what the values of variables choice1 and choice2 are?
I would be very surprised if you can, because you haven't assigned them values yet.

It's good to see that you are curious in an "extra credit" kind of way. I hope I've helped to answer your question. Feel free to follow up if you have more!

frisby


#4

"console.log(compare())"

Works great, I kept trying console.log(compare). Didn't fully dawn on me that you always need to include the parentheses. But thanks a lot a great help!


#5

Any question?

@rkdownie12gmail.com


#6

Thanks for your lengthy reply. I enjoyed reading through your links and you have helped me to understand what return and console.log's true purposes are. That as well as rcodeman's pointing out the correct syntax has really given me the confidence to carry on without having any gaps or unease about what I know.

I really hope you understand that I and I am sure others are very appreciative of the free help and advice you give.

To answer your question, I used the === operator to make choice1 equal to userChoice and choice2 equal to computerChoice inside the 'compare' function. I knew you were supposed to leave those two variables(user/comp.Choice) as comments but I wanted to push ahead and see how much of the game I could build myself with what i had already learned. I was really enjoying coding for the first time doing the functions as sad as that sounds lol, it felt like building lego or playing minecraft or something haha.

I assume that === is the correct operator because that means equals to and because the variables comp and user choice are global it should be able to call them into the function. However the = operator assigns a value so as far as I can tell in this instance both === and = will cause choice1/choice2 to share the same the value as comp and userChoice.

I am genuinely very grateful for your help and I hope you feel proud of yourself for the help you give.
Thanks and best regards
Ryan.


#7

No you have been fantastic and I really appreciate your help. Enjoy your day!


#8

its my pleasure :slight_smile:
thank u very much
@rkdownie12gmail.com


#9

Hi Ryan,

Thank you for your reply! I’m so glad to hear you enjoyed reading the links and that you have a better understanding about returns and console logs.

I was really enjoying coding for the first time doing the functions as sad as that sounds lol

I don’t think that’s sad at all! I’m just learning too, and I think going ahead of the lessons on your own sounds like a great approach to learning.


I used the === operator to make choice1 equal to userChoice and choice2 equal to computerChoice inside the ‘compare’ function

Perhaps I am misunderstanding but === is for comparison; it will compare one value to another and see if they are equal. (== does this, sort of, too, but if I understand correctly the problem with this is that if the two values are different types, e.g. a string and a number, one will be forced to become the same type as the other to sometimes unpredictable results, and is therefore best avoided). When you see === please imagine a balance/scale; rather than “equals to” think “are these equal?”

A single = is the assignment operator and makes “choice1 equal to userChoice” etc. as you describe above. Having said this, may I suggest you reexamine the first two lines of your function?


However if you recall variables from algebra class, lets say x = 2. The value of x is 2, and “x”, the variable, acts as a pointer that says, “Hey, when you see me, think ‘2’!”

Similarly, when we assign userChoice to choice1 we are saying that choice1 points* to userChoice. However, userChoice, as a variable itself, should also point to a value (in this case I believe they are both undefined, unless you uncommented the first part of the program).

Can you tell me where in the program we assign a value to userChoice? (Including the commented code in your answer is ok.) Similarly, where do we assign a value to computerChoice?

frisby


*I think it’s helpful to think of variables in this way, but this metaphor may not reflect how JS actually behaves. From JavaScript: The Definitive Guide

A primitive type [from MDN: Boolean, Null, Undefined, Number, String ,Symbol] has a fixed size in memory. For example, a number occupies eight bytes of memory, and a boolean value can be represented with only one bit. The number type is the largest of the primitive types. If each JavaScript variable reserves eight bytes of memory, the variable can directly hold any primitive value.[13]

[13]This is an oversimplification and is not intended as a description of an actual JavaScript implementation.

If the primitive versus reference type distinction is new to you, just try to keep the variable contents in mind. Variables hold the actual values of primitive types, but they hold only references to the values of reference types.

You may have noticed that I did not specify whether strings are primitive or reference types in JavaScript. Strings are an unusual case. They have variable size, so obviously they cannot be stored directly in fixed-size variables. For efficiency, we would expect JavaScript to copy references to strings, not the actual contents of strings. On the other hand, strings behave like a primitive type in many ways. The question of whether strings are a primitive or reference type is actually moot, because strings are immutable: there is no way to change the contents of a string value. This means that we cannot construct an example like the previous one that demonstrates that arrays are copied by reference. In the end, it doesn’t matter much whether you think of strings as an immutable reference type that behaves like a primitive type or as a primitive type implemented with the internal efficiency of a reference type.