Learn Introductory JavaScript by Building a Pyramid Generator - Step 55

Tell us what’s happening:

Hey, the Box tells me that i have to assign sum the value. But imo i gave sum a value with sum = a+b;
i tried to declare it inside the function, but than he is telling me, that i have to declare a sum variable.
i also tried to include the sum = a+b to return sum = a+b. but it doesnt fix the problem. can you help me?

Your code so far

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

function padRow(name) {
  return name;
}

// User Editable Region

let sum = 0;
function addTwoNumbers(a,b){
  sum = a + b;
  return sum; 
}
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/128.0.0.0 Safari/537.36

Challenge Information:

Learn Introductory JavaScript by Building a Pyramid Generator - Step 55

this is your function, do not use variables declared outside the function. If you want to use a variable inside the function, declare it inside the function

thx :slight_smile:
i changed it, but than he tells me, that i should declare a sum variable.

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

function addTwoNumbers(a,b){
const sum = 0;
sum = a + b;
return sum;
}
addTwoNumbers(5,10);

and after i changed it to a const, he tells me " 1. You should have a function called addTwoNumbers ." that confuse me a little bit

yes, you need a sum variable outside the function too, but it must not be the one changed by the function, you need to assign a function call to this one instead

you have a syntax error so all tests fails, you can’t change value to a variable declared with const

1 Like

Think about this line. What does it do? Where does the value returned by the function go? What if you wanted to use the sum later in the program or print it?

it gives the value 5 to parameter a and 10 to parameter b? The Value is saved in sum? But sum is just inside the function and it cant be used outside the function? Do i have to use sum if call the function?

Do you need to use sum inside the function?

Two hints:

You can return a calculation from a function, it doesn’t need to be a variable.

function(x, y) {
    return x**2 - y;
}

You can assign the result of a function to a variable:

let variable = function(x, y);
console.log(variable);
1 Like

thx that worked. but in generell i can also define it inside the function right?

You can declare variable inside the function if you want, however: variables declared inside the function cannot be used outside the function.

It depends what you are trying to achieve.

1 Like