Learn Introductory JavaScript by Building a Pyramid Generator - Step 55

Tell us what’s happening:

The hint states that I should declare the sum variable, Can You help me find the error in my code, What mistake did I make in this code??

Your code so far

const character = "#";
const count = 8;
const rows = [];

function padRow(name) {
  return name;
}

// User Editable Region

function addTwoNumbers(a,b){
  let sum = a+b;
  return sum;
}
console.log(addTwoNumbers(5,10));

// User Editable Region



const call = padRow("CamperChan");
console.log(call);


for (let i = 0; i < count; i = i + 1) {
  rows.push(character.repeat(i + 1))
}

let result = ""

for (const row of rows) {
  result = result + "\n" + row;
}

console.log(result);

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/126.0.0.0 Safari/537.36

Challenge Information:

Learn Introductory JavaScript by Building a Pyramid Generator - Step 55

they meant that the sum should be declared outside the function
the function itself does not need any variables other than a,b

let sum;
function addTwoNumbers(a,b){
sum = a+b;
return console.log(sum);
}
addTwoNumbers(5,10);

I have changed the code as you suggested,
But it’s showing the code should return the sum of the two variables as it is already showing the sum of those numbers??

you have the word sum in your code inside the function still but now it is not syntactically correct.
You don’t need it at all in your function.
You just need to use a and b in your function.

Define the sum outside the function.

Now I got the answer
Thank You I missed the clarity!
But, Can You confirm this one thing
Does it generally work if I declare the variable inside the function & logging it to the console?

Is the above code valid in general??

yes your initial attempt was a valid attempt at a function that sums two numbers.
It is just not what the step needed exactly.

1 Like

Generally you would not do this. You would either:

return a value
OR
console.log() a value.

Not both.

Here you create a variable sum and then return it. That’s ok syntax but it’s unecessary, it’s an extra step here. Instead of assigning a+b to sum you can just return it right away.

If you had a more complicated function you might use a variable like this.

1 Like

I completely got it, Thank you so much
I really appreciate you explanation

1 Like