-4

I am studying javascipt and meet a very small exercise like this.

Create a sum function with 2 parameters a and b. This is a function which mean they will make the summation between two numbers. The function will return the result of a + b.

Here is my code

function sum(a,b) {
c=a+b;
console.log(c);
}
writelog(sum);

But it said to me that there are some errors in my code, so could you please give me some ideas ? Thank you very much for your time.

4 Answers4

1

Your instructions say that the function should return the result, not print it. The printing should be done by the caller.

You need to supply arguments when calling the function.

function sum(a, b) {
  c = a + b;
  return c;
}
console.log(sum(5, 10));
Barmar
  • 741,623
  • 53
  • 500
  • 612
1

First, your code using wrong function console.log instead of writelog.

Second, your function should return value instead of console log inside it.

Third, when you call function need input parameter for it.

function sum(a,b) {
let c=a+b;
return c;
}
console.log(sum(5,6));
Hien Nguyen
  • 24,551
  • 7
  • 52
  • 62
0
function sum(a,b) {
  return a+b;
}
const answer = sum(5,6)
console.log(answer)
Muhammad Atif Akram
  • 1,204
  • 1
  • 4
  • 12
-2

WHy use writelog?. I suggest you to use console.log(). This is the link reference:

function sum(a , b) {
  const c = a + b;
  return c;
}

console.log(sum(5,6));
surya kumara
  • 185
  • 1
  • 11