1

I tried answering a simple question in code kata where the question goes like this:

Your goal is to return multiplication table for number that is always an integer from 1 to 10. For example, a multiplication table (string) for number == 5 looks like below:

1 * 5 = 5
2 * 5 = 10
3 * 5 = 15
4 * 5 = 20
5 * 5 = 25
6 * 5 = 30
7 * 5 = 35
8 * 5 = 40
9 * 5 = 45
10 * 5 = 50

here is the code I did but I used console.log

function multiTable(number) {
  // good luck
let arr = [1,2,3,4,5,6,7,8,9,10];
  for(i = 0; i < arr.length; i++) {
    let answer = arr[i] * number;
    console.log(arr[i] + '*' + number + '=' + answer);
  }
}

When I try it in the console window using dev tools it runs okay, but when I change console.log to return it only returns the last element multiplied to the function parameter i.e. if number == 5, it only returns 10*5=50 and not the whole iteration. Why is this happening?

3 Answers3

1

When you return something, you're breaking out of the function that's being called. When you print something to the console without returning, the function continues to run until the last loop executes.

In your example, the function will stop after returning the first result of:

arr[i] + '*' + number + '=' + answer

Where i=0 and number=5, so it will return 5.

Charlie
  • 11,380
  • 19
  • 83
  • 138
  • Oh so that explains it! Thanks a lot! – John Andrei Gayeta May 09 '21 at 13:19
  • So that means wherever the iteration is at that point, once `return` is called it automatically breaks out of the loop? – John Andrei Gayeta May 09 '21 at 13:26
  • 1
    Not just the loop, but the function entirely! I think it makes a bit more sense in [strongly typed](https://en.wikipedia.org/wiki/Strong_and_weak_typing) languages, where a function will return a specific datatype depending on how it's declared. So when you have a function defined to return an `int`, it needs to `return` an `int` at some point before reaching the end of the function. If you have a `void` function which isn't supposed to return anything (and just run), calling `return` will stop the rest of the function from executing. – Charlie May 09 '21 at 13:30
  • 1
    Yes, a `return` always immediately exits the function, no matter where it is placed inside the function. You could also have multiple `return` statements inside a single function. As soon as one is reached, the function ends. – Bart Hofland May 09 '21 at 13:32
1

You can return outside the loop like this.

function multiTable(number) {
  //  just minor changes.
  
let arr = [1,2,3,4,5,6,7,8,9,10];
let decorate = "" 

  for(i = 0; i < arr.length; i++) {
    let answer = arr[i] * number;
    console.log(arr[i] + '*' + number + '=' + answer);
    decorate = decorate + arr[i] + '*' + number + '=' + answer + '\n'
  }
  
  // outside of loop
  return decorate
}
Sermet Pekin
  • 357
  • 2
  • 6
1

Using return instead of the console.log will break the for-loop (edit: and the whole function!).

Instead, you could make a new array and push every answer to that array, and after the for loop return the new array.

Something like this:

function multiTable(number) {
// good luck
let arr = [1,2,3,4,5,6,7,8,9,10];
let newArr = [];
  for(i = 0; i < arr.length; i++) {
    let answer = arr[i] * number;
    newArr.push(arr[i] + '*' + number + '=' + answer);
  };
return newArr;
};