0

When I run this snippet, I get "4" as an output, but I want to return the value "4", 5 times.

Why is this and how can I fix it?

function addTwo(num){
  return num + 2;
}

function checkConsistentOutput(func, val){
  let first = func(val);
  let second = func(val);
  if(first === second){
    for(let i = 0; i < 5; i++){
    return first;
    }
  }else{
    console.log("This function returned inconsistent results");
  }
}

console.log(checkConsistentOutput(addTwo, 2));
Dave Newton
  • 158,873
  • 26
  • 254
  • 302
Luka Momcilovic
  • 159
  • 2
  • 16
  • 3
    The `return` statement causes its containing function to immediately exit. – Pointy May 02 '19 at 17:36
  • 3
    Because `return` returns. If you want to return multiple values you'll need to return an array or some other type of collection. – Dave Newton May 02 '19 at 17:37
  • Instead of returning just console.log inside of your for loop and then return at the end of the function – chevybow May 02 '19 at 17:37
  • Hm, so returning is almost like a break(); It exits the loop and bam, game over. – Luka Momcilovic May 02 '19 at 17:38
  • 3
    Not really; `break` just exits the loop. `return` exits the function. https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/return – Dave Newton May 02 '19 at 17:39
  • @Dave Newton thanks. Where would you say you go to get most of your knowledge? Mdn? Oreilly javascript? Codeacademy? – Luka Momcilovic May 02 '19 at 17:40
  • I’m still new to javascript – Luka Momcilovic May 02 '19 at 17:41
  • MDN is quite a good ressource to look up things. – Jonas Wilms May 02 '19 at 17:44
  • Possible duplicate of [Does return stop a loop?](https://stackoverflow.com/questions/11714503/does-return-stop-a-loop) – adiga May 02 '19 at 17:46
  • 1
    FWIW, the fact that `return` exits the function is not specific to JavaScript. Most languages work like that. `return` means to hand back control to the caller of the function, so being able to return multiple times from a *single* call doesn't make sense. – Felix Kling May 02 '19 at 17:48
  • @LukaMomcilovic The MDN docs, while not "official" like the spec is, it the reference I use most often--but things like this question are just basic JS language, so I'd continue down the educational path you're already on. That said, a primer on JS basics would probably be valuable, e.g., a book or tutorial, not just a class, which doesn't necessarily expose JS-in-general. – Dave Newton May 02 '19 at 20:21
  • @DaveNewton I'm doing the CodeAcademy course. You think I'm better off with a book instead? – Luka Momcilovic May 05 '19 at 15:28
  • @LukaMomcilovic ¯\_(ツ)_/¯ No way to know. Books have the time and bandwidth to go deep into fundamentals, while some courses may say "this is how you do XYZ" but not explain why or how. There's no "this *or* that"--use all the resources you have available. How "return" works is pretty fundamental and will be found in any JS resource that talks about functions. – Dave Newton May 05 '19 at 15:48

3 Answers3

4

The return statements stops the execution of a function (and returns a value if specified). This is non-negotiable.

That return value can be anything, a primitive, an object, a function, an array, etc. Maybe returning a 5-long array would suit your needs?

Alternatively, if you want to output a value, jump out of a function, but resume from the same place when called again, read about generators and the yield statement.

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Iterators_and_Generators

mbojko
  • 13,503
  • 1
  • 16
  • 26
0

Actually issue is, when you use return in for loop, it immediatly return from the loop, if you need 4 5 times you can use console.log() or may use a counter, which count your occurance of 4, and then from that counter you can gusse you condition pass or fail

for now, you can have a look to code snippet and see how console.log works

function addTwo(num){
  return num + 2;
}

//Is addTwo stable?
function checkConsistentOutput(func, val){
  let first = func(val);
  let second = func(val);
  if(first === second){
    for(let i = 0; i < 5; i++){
    console.log(first);
    }
  }else{
    console.log("This function returned inconsistent results");
  }
}

checkConsistentOutput(addTwo, 2);
Dupinder Singh
  • 7,175
  • 6
  • 37
  • 61
0

As other have pointed out, the return line is breaking the for...loop and returning the originating call. If you do need repeated returns from a function, you could try using a callback function to handle each interation.

function addTwo(num){
  return num + 2;
}

function checkConsistentOutput(func, val, callback){
  let first = func(val);
  let second = func(val);
  if(first === second){
    for(let i = 0; i < 5; i++){
      if ( typeof callback === "function" ){
        callback(first);
      }
    }
  }else{
    console.log("This function returned inconsistent results");
  }
}

checkConsistentOutput(addTwo, 2, function(res){
  console.log("Result: " + res);
} );