0

I just started learning JavaScript, when I ran the below code, it only returns orange, I thought it should return all the values.

function Array(whatever) {
    for (var i=0; i < whatever.length; i++) {
        return whatever[i];
    }
}

var list = Array(['orange','cat','mouse','dog','pen']);

The return is not working inside the for loop as expected, can anyone explain why?

BrutalDev
  • 6,181
  • 6
  • 58
  • 72
Llama
  • 1
  • 4
  • becoz the value is already returned... return break the loop... check this link https://stackoverflow.com/questions/11714503/does-return-stop-a-loop – Himanshu Bansal Mar 13 '18 at 05:40

4 Answers4

1

Q: What does "return" do?

A: It exits the function. Immediately.

BEFORE your loop has a chance to print anything else besides "orange".

You should NOT call return until you're "finished" with everything the function needs to do.

SUGGESTIONS:

Array" is probably a poor name from a function.

What is it you want the function to do?

For example, maybe you want it to "print array". In that case:

function printArray(whatever) {
  for (var i=0; i<whatever.length; i++) {
    console.log("whatever[" + i + "]: " + whatever[i]);
  }
}

var list = Array(['orange','cat','mouse','dog','pen']);
printArray(list);

Familiarize yourself with MDN (Mozilla Developers Network).

Here's a great introductory tutorial:

https://developer.mozilla.org/en-US/docs/Learn/Getting_started_with_the_web/JavaScript_basics

paulsm4
  • 114,292
  • 17
  • 138
  • 190
  • I think your code is here, if we console.log here, does that mean the function did not return the value, since console.log is for displaying purpose, sorry, I am still learning, please correct me if I am wrong. function printArray(whatever) { for (var i=0; i – Llama Mar 13 '18 at 08:54
  • A "function" 1) always "does something", and 2) may "return something". Your original problem was that you did a "return" prematurely. My example "does something" ... on EACH element. But it doesn't happen to "return a value". Q: Does that answer your question? Q: If you *wanted* to "return a value" - what would that value be? And why? – paulsm4 Mar 13 '18 at 16:58
  • Hi Paul, thanks. A: It does answer my question, but I was looking for return value. If I am looking for return value, then I think the return value would be printArrary, I am guessing the output should be argument ['orange','cat','mouse','dog','pen']. – Llama Mar 13 '18 at 17:19
  • I could be wrong, I remember return is capturing the value that come back out of the function, console.log is printing on the console. – Llama Mar 13 '18 at 17:27
  • A function can have a "return value". That's what the "return" keyword is for ;) It can be any value: a string (like "orange"), an array (like "list") or anything else you want it to be. But again: 1) a function can "do something", and it can also "return something". 2) In your case, it looks sufficient to "do something". 3) But if you wanted to "return something" - sure. It's your choice, it's completely up to you :) – paulsm4 Mar 13 '18 at 21:21
0

You must call return in end of your function

function Array(whatever) {
  var arr=[]
 for (var i=0; i<whatever.length; i++) {
   arr.push(whatever[i]); /// because you write here return then function exit
}
 return arr;
}

var list = Array(['orange','cat','mouse','dog','pen']);
aimprogman
  • 294
  • 2
  • 3
  • 16
  • @Lemo tree It is resolve your problem? – aimprogman Mar 13 '18 at 05:56
  • Hi aimprogman, thank you, yes. Just want to clarity that adding push here is to add one more value to the end of an array every time the loop runs, then it returns to the new length of the array. Is that correct? sorry i know it is a basic question..-_-!! Just started learning JavaScript 2 weeks ago. Also, Is "var arr=[ ]" meaning to start with any value? Thank you very much! – Llama Mar 13 '18 at 08:42
  • `var arr=[]` means "initialize array "arr" with zero values. Create an "empty array". – paulsm4 Mar 13 '18 at 21:22
0

Function should only return after the for loop.

function Array(whatever) {
var tmpArr = [];
    for (var i=0; i<whatever.length; i++) {
        tmpArr.push(whatever[i]);
    }
return tmpArr;
   }

var list = Array(['orange','cat','mouse','dog','pen']);
0

Dont use return inside the for loop. once return statement execute it will come out of function and the remaining statement of the function will not execute.

//no need of for loop here
function Array(whatever) {
return whatever;
}
//if you want it line by line
function Array(whatever) {
for (var i=0; i<whatever.length; i++) {
    console.log(whatever[i]);
}
}
John Willson
  • 444
  • 1
  • 3
  • 13