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
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.
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?
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.