FAQ: Code Challenges: Intermediate JavaScript - squareNums()

This community-built FAQ covers the “squareNums()” exercise from the lesson “Code Challenges: Intermediate JavaScript”.

Paths and Courses
This exercise can be found in the following Codecademy content:

Web Development

FAQs on the exercise squareNums()

Join the Discussion. Help a fellow learner on their journey.

Ask or answer a question about this exercise by clicking reply (reply) below!

Agree with a comment or answer? Like (like) to up-vote the contribution!

Need broader help or resources? Head here.

Looking for motivation to keep learning? Join our wider discussions.

Learn more about how to use this guide.

Found a bug? Report it!

Have a question about your account or billing? Reach out to our customer support team!

None of the above? Find out where to ask other questions here!

Newbie here… getting frustrated with what appears to be correct code but fails to be recognized as correct. Which means that if I don’t do it exactly as the system has it, I can’t move on… talk about taking the wind out of your sales as a new programmer.

In this exercise I used the following as my function:

const squareNums = (array) =>{

I eventually gave in and asked for the solution which looks similar (spoiler alert) albeit all on one line.

I understand that passing one array does not require the parentheses while they are required when passing zero or multiples, however, in previous exercises when I didn’t include them, my code failed. Based on that, I’ve been including them.

The error message at the bottom of the code window kept popping up with “did you pass a function into a map() that takes a number and returns is square?” I thought I had, but it wouldn’t pass me on to the next exercise.

Is my code syntax correct, even if it doesn’t match the book? Suggestions on how I should be approaching future exercises on Codecademy?


In this particular example, your syntax is a bit off :slight_smile:

With arrow functions, if you include curly brackets after the arrow, you need to explicitly write return to return anything from the function. If the only statement in your function is a return statement, then you can remove both the return and brackets.

Because your function has curly brackets but no return before array.map(toSquare), it effectively throws away the result of map, and just returns undefined.

So you can either remove the brackets or add a return to get the function working.


Nothing is returned, literally. JS interprets this as undefined, not the actual return value.

1 Like

Hmm, I thought undefined would be the actual return value based on Mozilla’s article about functions:

A function without a return statement will return a default value. In the case of a constructor called with the new keyword, the default value is the value of its this parameter. For all other functions, the default return value is undefined.

Or are we saying the same thing? Just curious as I’m pretty new to Javascript.

 > function foo() {

<- undefined
 > foo() && 'a'
<- undefined

It appears my understanding may be lacking. In Python the above would echo nothing to the console. My only question still remains, where is undefined injected into the data stream? At the function? Or at the caller?

1 Like

where is undefined injected into the data stream? At the function? Or at the caller?

I suppose the function since it is the return value. I tried debugging this in VS Code:

function doThings() { 
  const x = 'abc';


When the debugger reaches the closing bracket of doThings, “return value: undefined” is displayed in the Variables window.

Interestingly, it seems Python is actually similar...

…but its default return value is None. I found this in the Python 3 documentation:

In fact, even functions without a return statement do return a value, albeit a rather boring one. This value is called None (it’s a built-in name). Writing the value None is normally suppressed by the interpreter if it would be the only value written. You can see it if you really want to using print()

The return statement returns with a value from a function. return without an expression argument returns None . Falling off the end of a function also returns None .

When I run this, I do see “None” in the console:

def doNothing(): pass
1 Like

So in other words, a value is returned? That would mean an implicit return in all cases. Then why require a return keyword at all? Ruby seems to have figured this out, but JS is only fudging it, as far as I can make out.

1 Like

I don’t know if it’s considered an implicit return, since it just returns undefined and not the value of the last expression in the function.

In Ruby, this prints “hello world”, like you’re indicating:

def doStuff()
   'hello world'
puts doStuff()

Whereas in JavaScript, this just prints “undefined”:

function doStuff() { 
  'hello world';

I think in Javascript, the only functions with implicit return values are arrow functions with no curly brackets. From Mozilla’s arrow functions documentation:

Arrow functions can have either a “concise body” or the usual “block body”.

In a concise body, only an expression is specified, which becomes the implicit return value. In a block body, you must use an explicit return statement.

So this function with no curly brackets does print “hello world”:

const doStuff = () => 'hello world';

But this, with brackets, prints undefined again:

const doStuff = () => { 'hello world' };
1 Like

Python treats nothing like nothing. There is no indication in the response that anything was returned. Only if we print it will it appear as None. As an object it doesn’t even qualify a newline. All we get is a command prompt in response. There is no indication there was a response to echo.

Mathematicians racked their brains around the idea of nothingness, and a little background research will reveal what a slam dunk it was once zero came into the picture.

The founders of Calculus are not Leibniz or Newton, but a host of others along a path from which they inherited. Both had amazing insight into nature, though I venture Newton excelled here, and both had amazing insight into maths. Neither can be seen to exceed the other, though historically one is held the victor.

There was never anything amiss about the RPN of the Hewlett Packard calculators of the seventies.


Some stuff doesn’t survive economic forces, but it still deserves to survive historically. Particularly when we consider how we program.

a = 6
b = 7

We load the registers first.

return a OP b

We apply the operator second.

Typical programming stuff that just never took hold in hand held calculators. Pity.

Mind, today we don’t need calculators…


I passed the code, however the result is not correct.
Here is my code:

const numbers = [2, 7, 9, 171, 52, 33, 14]

const toSquare = num => num * num

// Write your code here:
const squareNums = arr => arr.map(toSquare);

The arr still returns the original values, not the squared ones.
Please advise.

Your note was so informative, I did not realize the arrow syntax about the return with/without the bracket.

I was also in trouble solving this exercise. I’ll give some guidance to whom might need it - I used arrow function, into which I wrote .map method, just like in the former exercises we solved regarding .map. And I also had to write “return” twice for the code to work and to let me pass.

I quickly completed this exercise with the following… but this answer wasn’t accepted.
Can anyone help?

const squareNums = numbers.map(toSquare);


[ 4, 49, 81, 29241, 2704, 1089, 196 ]

While the map method is uniquely designed for just such a purpose, it may not have been introduced yet, so another approach was expected. Many lessons are like this in that jumping ahead is not expected. We must stay with the program and rely upon naive approaches being taught.

Thank you for your response @mtf

1 Like

We might think back to such cliche responses as, “Don’t be naive!” The term in programming is not so pejorative. It actually refers to the in-built logic that we all possess in one form or another. Those who have discovered it within themselves will have less of a hurdle, but most will still form their own logic. At times it is sufficient to meet the needs. That’s naive code.

1 Like

This is returning the squared numbers, but not passing the test. help?
Error: Does your function call map() on the array that was passed into it?

const numbers = [2, 7, 9, 171, 52, 33, 14]

const toSquare = num => num * num

// Write your code here:
const squareNums = (array) => numbers.map(numbers => {
  return toSquare(numbers)

const squareNums = (array) => numbers.map(numbers => {
  return toSquare(numbers)

Your formal parameter does not match the object on the map() method. A function should work with its parameters, not an outside object (global) else it will not work universally on any array, only that one, regardless what is passed in.


Above we pass the callback function by reference to the map iterator method. It has its own internal iteration variable which is passed to the callback on each element in the array.

So we have two functions…

    // the callback
    const toSquare = x => x ** 2;

    // the mapper
    const squareNums = array => array.map(toSquare);

When we pass numbers to it, we get,

[4, 49, 81, 29241, 2704, 1089, 196]

What might be confusing to a reader of your code is that there are two numbers variables that each mean something different.

* an array of values
* a value in the array

Were we to revise your code slightly,

const squareNums = array => array.map(number => {
  return toSquare(number);

We can see that it will work, as expected, barring that it can still be refined. Note that in your approach, toSquare() is not a callback, but a standalone function. The callback in that case is,

number => {
    return toSquare(number);
1 Like

While majority of our sample exercises in JavaScript here that I don’t even know what’s going on behind the scenes so I thought sharing this website might be helpful to students to visualize code like myself:

http://pythontutor.com/javascript.html#code=const%20shoutGreetings%20%3D%20arr%20%3D>%20arr.map(word%20%3D>%20word.toUpperCase()%20%2B%20’!’)%3B const%20greetings%20%3D%20[‘hello’,%20’hi’,%20’heya’,%20’oi’,%20’hey’,%20’yo’] console.log(shoutGreetings(greetings))&curInstr=0&mode=display&origin=opt-frontend.js&py=js&rawInputLstJSON=[]

Thanks for the View Solutions shortcut otherwise I cannot progress to the next exercise like this squareNums(). I’ll continue continue and repeat repeat this JS ES6 until it goes to memory. Any learning tips beyond hard work and determination?