This forum is now read-only. Please use our new forums! Go to forums

banner
Close banner
0 points
Submitted by y3ll0wspr4yp41nt
about 9 years

"Return"?

I’m well into “Rock, Paper, Scissors” right now but the command “return” is lost on me. I’ve been instructed to use it and I can but I don’t really know how it works or what it does.

Answer 5517210b51b887b82c002114

16 votes

Permalink

return is to a function as break is to a loop. It terminates the function and returns to the caller. The one difference is that break has no value attached, whereas return does.

Eg. return; // return value === undefined return a; // return value => whatever ‘a’ references return console.log(“return value can be a function”);

In practical use,

var compare = function(choice1, choice2){
    if (choice1===choice2){
        return "The result is a tie!";
    }
};
console.log(compare('rock','rock'));
console.log(compare(3,5));

Output

The result is a tie!
undefined

Notice that the last call is not returned with a direct statement. In JavaScript functions, return is implied when not written explicitly. The value is always undefined.

points
Submitted by Roy
about 9 years

15 comments

nathan.goodnews about 9 years

That worked, but I have no idea why. The javascript lesson does not seem to be for absolute beginners. Something is missing in the explanations. So, I will go back and start it again.

Thank you for your help

crashoveride247 almost 9 years

it’s more like the instructions for the console.log test is missing

Roy almost 9 years

The author leaves that up to us. It is not a requirement in the exercises, just the compare function, properly functioning.

Please help, what is wrong with my code? 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”; } console.log(choice1, choice2); };

I mean this pls. 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”; } };

Roy almost 9 years

Please ‘Ask a question’ in a new thread, and post your code there so we can read it, and the whole community can help address it. This thread has only three subscribers and the opening post has been resolved. Thank you.

Uncle Ben almost 9 years

in the compare() function listed above why is “ return “The result is a tie!”;” used instead of console.log”The result is a tie!”;?

Roy almost 9 years

So that the code is portable. If we use return we can send the output to the browser window, where most users are going to see it. If we use console.log() we do not have portable code since the output is not in the browser window, but in the console, a debugging tool that most people don’t even know exists. This is preparing us for when we begin to work in a webpage, not the debugging console.

Roy almost 9 years

In my first five years of coding, I didn’t even know the console existed. That’s a fact. Everything was sent to the browser window. I did all my debugging live. Today, we hope you will not be so daring or reckless. That’s why we are learning how to use the console now, and the webpage later. So we know it exists, for one, and how to use it, for two.

Uncle Ben almost 9 years

I haven’t gotten to the point in my training where I know how to use notepad to play around and experiment with Javascript. What would I have to do in notepad so that I could? Also, what is a “portable” code?

Roy almost 9 years

Portable code is essentially modular and can re-used in several situations within a program, or it can be dropped into another program and used there with little or no change to the module. When we use console.log() we give our program a dependency–the JavaScript console. When we use string and number literals in our program we limit the range the program may operate within. Dependencies and fixed values both work against making code portable. As for Notepad, it is just a text editor. We may write code and save it as script.js (or anyName.js) then run it in a browser, the details of which will take a little while longer to learn so I won’t go there, just now. Keep up with your lessons and this will come up in due course of time.

Mohammad Daud Ibrahim over 8 years

In this stage of the course it very difficult to grasp the perfect idea of JAVA SCRIPT. My question is “will by the end of the course would i have a sound understanding of JS

Roy over 8 years

Short answer? Sound? No. Basic? Yes. This is just a starting point, nothing more. We get our feet wet and determine if we have the aptitude and determination to follow up with some serious learning or perhaps pursue a career in CS or Web/App Development.

Mohammad Daud Ibrahim over 8 years

Thanks very much for replying. I am currently a first year student of Business Information Technology Heres the course http://www2.gre.ac.uk/study/courses/ug/it/g510

Roy over 8 years

You should know that JavaScript and JAVA are the same in name only. That’s pretty much where the similarity drops off. JavaScript is a prototypal language that emulates an Object Oriented Programming environment, whereas JAVA is Class based and is truly an Object Oriented Programming language. I’ve heard tell there may be a JAVA introductory course coming soon to CC, but in the meantime, suggest you pour yourself into your course and set this aside before it causes too much confusion.

Answer 553b391751b887b4bd0000e1

11 votes

Permalink

var compare = function(choice1, choice2) { if(choice1 === choice2) { return “The result is a tie!”; } }; console.log(compare(‘rock’,’rock’)); console.log(compare(‘scissor’,’scissor’)); console.log(compare(‘paper’,’paper’)); THIS RETURNS “The result is a tie!” without undefined..

points
Submitted by earago15
almost 9 years

2 comments

Roy almost 9 years

In the above example, these are the only returns that are not undefined. Any others will be.

Hax0rz almost 9 years

works the whole lesson #lesson Hack

Answer 55dabb49e39efe58ca000675

1 vote

Permalink

My code returning and error:

var compare = function(choice1, choice2) {
  if(choice1 === choice2) {
    return 'The result is tie!';
  }
};
console.log(compare('rock','rock'));
console.log(compare('scissor','scissor'));
console.log(compare('paper','paper'));

Error message:

Oops, try again. Your compare function does not return 'The result is a tie!' when there is a tie. 
points
Submitted by Lantos István
over 8 years

2 comments

Roy over 8 years

The SCT might be looking for ‘scissors’.

John over 8 years

If you look at the the line:

return “The result is tie!”;

you will see that its not the same as the actual statement that the console is checking for which is:

“The result is a tie!”;

You are missing the “a”

I think that could be the problem.

Answer 55e2456186f55210b9000020

1 vote

Permalink

Well Try this one: var userChoice = prompt(“Do you choose rock, paper or scissors?”); var secondChance =prompt(“Are you sure you wonna be : “+ userChoice + “?”+ “ ,Type Yes or No”); if(secondChance == “No”){ userChoice = prompt(“Do you choose rock, paper or scissors?”); } else if(secondChance == “Yes”){ alert(“You Choose: “+ userChoice); } console.log(“You selected: “+ userChoice); var computerChoice = Math.random(); if (computerChoice < 0.34) { computerChoice = “rock”; } else if(computerChoice <= 0.67) { computerChoice = “paper”; } else { computerChoice = “scissors”; } console.log(“Computer: “ + computerChoice); var compare = function(choise1, choise2){ if(choise1===choise2){ return(“The result is a tie!”); }
};

points
Submitted by Konstantin F
over 8 years

Answer 552f86ef95e378b16a0001db

0 votes

Permalink

If you want you can think of functions as sub programs. They know the context of the main program (they have access to its variables if not shadowed and may receive addition information by parameters) but apart from that they are a separate program.

The start command of this sub program is the function call which interrupts the main program and jumps to the beginning of the sub program and then it runs as long as you want it to. The job of return is now to manage the return from the sub program to the main program. As Roy already mentioned you now have 2 options either let the program run until no statements are left, then it will automatically return with a value of undefined. Or quit the function manually by using return which directly leaves the function changing the value to whatever is mentioned after it (nothing = undefined).

@Roy returning a function would be return console.log or return compare. This:

return console.log("return value can be a function");

would run the console.log function -> print the text and then returns its value, which is undefined.

Last but not least, return is often confused with an output to the screen, but that is not its purpose. The console has the feature that it echos the last value unless its undefined, so if the last statement is a function call that returns a value, that value is echoed. But as you can see by using more than one function call this only works for the last of them. So better just use it as a value e.g. store it in a variable:

var result = Math.random()*5+1;

or use a real output function to print it to the screen.

console.log(compare('rock','rock'));
points
Submitted by haxor789
about 9 years

5 comments

Roy about 9 years

I love that word, ‘interrupt’. Precisely… return console.log(“return value can be a function”);. console.log() was my return value. I already know that at the caller end it will be undefined, but I thought that point was made.

I don’t really get that last bit. It’s kind of moot, I thought.

Roy about 9 years

Think I do too much visualizing and not enough thinking…

haxor789 about 9 years

“I already know that at the caller end it will be undefined, “ sry made the impression that it is an example for another group of return values. “ I don’t really get that last bit. It’s kind of moot, I thought.” it is, but I’ve read so many post that compare console.log to return that I think it doesn’t harm to mention it :)

Roy about 9 years

And I would not store any value that is meant to be transient. Does this program need a record of who won? No. A transient statement leaks no memory. A variable does; and it adds up if there is no deliberate garbage collection or variable usage rigor that sets everything to null, now and then.

haxor789 about 9 years

Good point I thought of it more as an example on what to do with a returned value in general, but with the name of the function it doesn’t make much sense here…