0

I'm experimenting with functions. I know nested functions are possible, but seeing if someone could help with syntax errors

CHALLENGE: Create a function that takes a number, and creates an array from every number up to that number. But return the square of each number as an array, then sum them up. Return the sum.

function squaredSum(number) {
   var squaredNumberArray = []
   
   for (var i = number; i > 0; i--) {
       squaredNumberArray.push(i**2)
   }  var sum = function(squaredNumberArray) {
        var summation = 0
       
        for (var x = 0; x < squaredNumberArray.length; x++) {
            summation = summation + squaredNumberArray[x];
        } 
    } return summation;
}

console.log(summation);

1
  • Your code is not even syntactically ok. Make sure your brackets are in correct places (like the one on the line with return statement), also make sure you call your functions correctly(i.e. your sum function) Commented Feb 25, 2020 at 23:05

2 Answers 2

1

Okay first, your code is a little bit of a mess there, you should probably indent it.

The function you assign to the variable "sum" must return the "summation". However, you put the return outside the scope of that function.

Then, the squaredSum function must return what the sum function returned (redundancy) and must receive a parameter to work with (in this case, the squares array).

    function squaredSum(number) {
      var squaredNumberArray = []

      for (var i = number; i > 0; i--) {
        squaredNumberArray.push(i**2)
      }  

      var sum = function(squaredNumberArray) {
        var summation = 0
        for (var x = 0; x < squaredNumberArray.length; x++) {
            summation = summation + squaredNumberArray[x];
        }
        return summation;
      }

      return sum(squaredNumberArray);
  }
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks everyone for all of your input! I know what I gave was a bit of a mess.. I've been practicing for 2 weeks, so it's taking some time to learn all the proper syntax and best practices. But this definitely helped me learn a good bit. Question, Ben, I thought declaring a const variable made it unavailable to change? Like when you declared const squares = [], what are the rules for mutation at that point? We can seemingly add elements, but what else?
Once a const identifier has been declared and assigned it cannot be reassigned. So it is the link between identifier and value that is “constant”. If it points to an object, then that object can be changed at will.
1

Some alternative implementations, if I understand the problem correctly.

function sumSquares(n) {
    const squares = []
    let sum = 0

    for(let x = 1; x <= n; x++) {
        squares.push(x*x)
    }

    for(let x = 0; x < n; x++) {
        sum += squares[x]
    }

    return sum
}

const result = sumSquares(5) // 55

console.log(result)

Alternatively: create an array of length n. Spread the array into a new array to populate the keys.

Use the keys as the integers; use map to square them, and then reduce to sum them.

const sumSquares = (n) => 
    Object.keys([...Array(n+1)]).map(k => k*k).reduce((acc, c) => acc+c, 0)

const result = sumSquares(5)

console.log(result) // 55

Correction of your existing implementation:

function squaredSum(number) {
    var squaredNumberArray = []

    for (var i = number; i > 0; i--) {
        squaredNumberArray.push(i ** 2)
    }

    var sum = function(squaredNumberArray) {
        var summation = 0

        for (var x = 0; x < squaredNumberArray.length; x++) {
            summation = summation + squaredNumberArray[x]
        }
        return summation
    }

    return sum(squaredNumberArray)
}

console.log(squaredSum(5)) // 55

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.