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;
}
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.
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:
Your function should return the sum of the two numbers.
Your function should not return a hard-coded value. That is, it should work with any two number arguments.
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