Back to Interview Prep

Warm Up · Function, if-else

Sum of Two Integers

Problem Statement

Write a program that defines a function to calculate the sum of two integers and prints the result. Call this function by passing two integer values.

Example

Input: 5, 3

Process: a + b => 5 + 3 = 8

Output: 8

Approach

  • Define a function that takes two numbers as input.
  • Add the two numbers inside the function.
  • Call the function with two integers & print the result.

Visualisation

1Define a functionfunction Sum(a, b)
2Add the numbersreturn a + b;
3Call the functionSum(5, 3);
4Print the resultconsole.log(result);

Explanation

  • Sum(a, b) is a function that takes two arguments.
  • Adds them and stores the result in a variable named add.
  • Prints the result.
  • Sum(a, b) calls the function with a=5 & b=3, so it prints 8.

Your solution

Solve it in JavaScript and print the answer with console.log(). Run to see your output, then Submit to check it against the test cases.

// Write your solution in JavaScript
// Print the answer with console.log()


Console

No output yet. Use console.log() to print values here.

Write your own solution and press Submit once to unlock the answer.

You're viewing a free lesson. Sign up free to track your progress and unlock the full course.Sign up freeSign in