I need help please

I am working on two assignments

  1. Create a function called sum that returns the sum of two numbers passed in as parameters.
    this is my code
    var sum = function(2)(3) {
    return (5)

2.Create a function called triple that takes in a number as a parameter, triples the number, and returns the result.

var triple = function(2) {
return (6)

When you are defining a function you do not pass in the literal values. Instead you define it with variable names because the value you pass in might be different each time. You are passing in a number so instead of having a literal “2” replace that 2 with the a sensible name for a variable that stores a number. The word “number” or “num”, “x”, “a” are all good candidates.

so for example if I wanted to quadruple a number using a function I would go:

var quad = function(num) {
    return num * 4;

You see that “num” is the variable and how it is put in the function parameters: between the parentheses?

After the function has been defined you can now call it a bunch of different times:

var a = quad(1); 
var b = quad(5); 
var c = quad(42); 

You would have been shown something similar in the assignment: a function that is close to what you need that you have to change to make it do what you want. Taking functions and modifying them helps you learn how to write your own.

Your function that has 2 parameters is also incorrect in another way. You dont need to close your parentheses and open them again for each variable in your parameters, instead you separate them with a comma.

For instance:

function foo(a,b) {
//do something

Create a function called sum that returns the sum of two numbers passed in as parameters.

var sum = function(2,3) {
return (5)
I ran the code like this but it still told me I am incorrect maybe I’m not fully grasping this.

Hey berg,

Values are passed through to a function using a call in the main area of the program. For example, there will be a function call like below:

var sumResult = sum(2,3);

This will call the sum function, passing through 2 and 3 to the respective variables on the function. You will need to write the function to receive these variables, perform a task on them and return the result as below:

function sum(a,b){
//write the code to add a and b here
//write the code to return the sum of these added variables here

Some challenges that may help are below:

Hope that helped clear up your confusion!

It did help but I am still a little confused this is what I wrote but now it is telling me Error >>>Expected’;’

Function sum = function(2,3) {
var 2+3
return (5)

Hi berg25:

To answer your question, it is dealing with your syntax.

You need to first declare your function and pass in you parameters, i.e.
> function sum(paramter1, parameter2) {

// add code here

Inside your function you usually want it to return the result of some code
in this case you want to add your two parameters.
So here you will return the result of the two parameters with the addition operator.


function sum(number1, number2) {
return number1 + number2;

Don’t confuse parameters with arguments, the parameter is just a place holder that is given its value when you pass in a value as arguments during the function invocation.

so in this case we will invoke the function add and pass in the two arguments 2 and 3



the result of this function call will be 5

I hope this answers your question, please feel to reach out if you need any help and I can do a quick hangout with you to answer some of your questions.

1 Like

You misunderstand how functions work. You do not write a function that adds 2 and 3 and returns 5. You must write a function that adds x and y and returns the result. I only use x and y as an example, it does not matter how you call them in your function.

In this example I write a function that multiplies x and y:

function multiply(x, y) {
  var product = x * y;
  return product;

x and y are parameters of multiply. Parameters are always variables like x, never literal values like 2. What use would a function have if it could only add 2 and 3 and no other numbers? If we use placeholders instead of numbers, we can add any number to any other number.

Now I can use that function and give it actual numbers: var test = multiply(2, 3);
test is now 5.

How to write a function:

You try to define a function like this: Function sum = function(2,3) {...};, which is wrong.
There are two ways to define a function, either:

function sum(a, b) {
  // some code


var sum = function(a, b) {
  // some more code

Note that the second variants ends with a semicolon, while the first does not.

Where to use the semicolon

I find this hard to explain, but I will try. I think there are two basic rules:

  1. Don’t end a { } block with ;
    function test() { // do something... }

  2. End everything else with ;
    var myNumber = 42;

A bit of a corner case is this already explained function definition:
var sum = function(p, q) { // do something... };
This is an anonymous function, because it does not have a name. It is assigned to the variable sum, but that is not the same as writing function sum(p, q). Anonymous functions don’t count as blocks when it comes to semicolons, so you should still add one at the end of the line.

1 Like

Personally I’m partial to the “don’t use semicolons” philosophy, but since @QuincyLarson has said that the FCC code uses the airbnb style guide, I’ve gone back to using them (under protest) :wink: