Steamroller: Why does this work? I thought a return statement exited a function?

Tell us what’s happening:
Hi, I don’t understand the following line of code from this solution:

return {};

The W3School’s website gives the following definition of a return: The return statement stops the execution of a function & returns a value.

In this code, return seems to move map() to the next element? Rather than stopping the execution of the map() function?

  **Your code so far**

function steamrollArray(arr) {
return arr
  .replace(",,", ",") // "1,2,,3" => "1,2,3"
  .split(",") // ['1','2','3']
  .map(function(v) {
    if (v == "[object Object]") {
      // bring back empty objects
      return {};
    } else if (isNaN(v)) {
      // if not a number (string)
      return v;
    } else {
      return parseInt(v); // if a number in a string, convert it

steamrollArray([1, [2], [3, [[4]]]]);
  **Your browser information:**

User Agent is: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/99.0.4844.84 Safari/537.36

Challenge: Steamroller

Link to the challenge:

1 Like

That return statement is inside of the callback function for the map, so only that function is being returned from.

It can sometimes be handy to compare solutions… What does your solution for this challenge look like?

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.