Learn Introductory JavaScript by Building a Pyramid Generator - Step 55

Tell us what’s happening:

I have done this several times and can’t figure it out. do I need const infront of sum? I’m somewhat confused on if the sum variable goes inside or outside of the addTwoNumbers function. And I am not clear on how to add them. I have tried it numerous ways. So I’m reaching out for help. I have erased what I had yet again and I started over. So here is what I have currently.

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

sorry editing:

the addTwoNumbers needs two variables in the parameter area
so erase 5, 10 and put 2 variables instead

also check your console for an error message.

function addTwoNumbers(num1, num2) {
  sum = 5 + 10;
  return sum;
}

is this what you mean? still not working. i just feel lost at this point

The function now has two parameters.
So far so good.
Now you have to add those parameters up in your function and return the result. Erase the 5 and 10 again.

Ok great! thanks for walking me through this. These are times I wish I was in class lol. Here is what I have now. What am I still doing wrong?

function addTwoNumbers(num1, num2) {
  sum = num1 + num2;
  return sum;
}
addTwoNumbers(5, 10);
console.log(sum);

You are doing just fine. You just need to add a let in front of the word sum when you declare it.

Also to print the sum outside the function, call the function inside the parenthesis of the log statement.

Check the console as you code to see errors that point out when you are trying to log something that doesn’t exist.

Remove the sum variable from your function
It should look something like this

function myFunc(a, b) {
 return a * b
}
let product = myFunc(10, 6)
console.log(product)

This function would output 60.

Hope this helps!
Happy coding

thank you so much for your help and support. so when im declaring the variable it should be outside of the function. got it! so the function is just describing what i want to happen. when i declare the variable and call it, thats when i will get the return or actual thing i want the function to do. is this correct?

here is my new code

function addTwoNumbers(num1, num2) {
  return sum;
}
let sum = addTwoNumbers(5, 10);
console.log(sum);

I’m still getting the following errors:

  1. Your function should return the sum of the two numbers.
  2. Your function should not return a hard-coded value. That is, it should work with any two number arguments.
  3. You should declare a sum variable.
  4. Your sum variable should have the value 15

I figured it out. the return needed to have the equation that i wanted, i.e. +. so when i changed it to return num1 + num2 it passed.

thank you again for your help.

1 Like

for future reference, not using const or let will declare a “global” variable, which means it doesnt matter if it is inside or outside the scope of your function. this is bad practice because it can make your code error prone and hard to debug.

if you add 'use strict'; to the top of your file, it wont declare the variable at all and return a reference error.

const or let will both work fine. the only difference is const is immutable (unchangeable), so if you initialize (give a value to) a variable, you wont be able to change it later on. let is good for general use because its less strict but still maintains local scope. statements that use { } is what is meant by scope. global scope means everyone has access to it

let a = "alice" // declared in global scope

function foo {
    let b = a // sets b to alice because its within the scope of a

    if (true){
        let c = "bob"
    }

    let d = c // doesnt set d to bob because its outside the scope of c
}

also you can declare variables with function parameters that will only work inside that specific function

// declares a variable called name
function person(name){
    return name
}

anything you pass into person() will be set at name

person("joe") // returns joe