'How would I have this sum print out it works

function testSum(){
    var expected = 7
    var actual = sum(5, 2)

    if (actual != expected) {
        console.log("It's broken..")
    } else {
        console.log("It works!")
    }
}

I don't know how to work this out, please help



Solution 1:[1]

In a python programming language, you can use sum as the sum of array elements such as sum(iterable, start). However, in javascript sum is "undefined" unless you define that function. Just sum as 2+7.

Solution 2:[2]

Your code gives the expected output after calling the function. Though you haven't provided details on your sum function. So I can't speak on whether your sum function is returning the correct result.

I gave an example implementation of sum that checks whether a and b are numeric.

function sum(a, b) {
  if (!isNaN(a) && !isNaN(b)) 
  {
    return a + b;
  }
}

function testSum(){
  var expected = 7
  var actual = sum(5,2)

  if (actual != expected) {
      console.log("It's broken..")
  } else {
      console.log("It works!")
  }
}

testSum();

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 configtheworld
Solution 2 Jaxon Crosmas