nathan
nathan

Reputation: 379

writing a pass test in javascript need a review for my code

I am learning and practicing to write a test:

Here is the test case add-numbers.test.js:

var addAllnumbers = require("./add-numbers");
test("Add all numbers", function () {
  var numbers = [9, 23, 10, 3, 8];
  var expected = 53;

  var output = addAllnumbers(numbers);

  expect(output).toEqual(expected);
});

here is add-numbers.js:

function addNumbers(numbers) {
  var sum = 0;
  for (var i = 0; i < numbers.length; i++) {
    sum += numbers[i];
  }

  return sum;
}
module.exports = addNumbers;

Did I do it right?

Upvotes: 0

Views: 248

Answers (1)

Dylan Cadd
Dylan Cadd

Reputation: 20

I believe that looks right! Can I ask why you are using the .required() instead of having that function in the same file? Either works, just curious.

Also, you should try to incorporate modern syntax whenever possible (that is if you can understand exactly what is happening).

But the following method does the same as yours but doesn't require another method:

let numbers = [9, 23, 10, 3, 8];
let expected = 53;
    
var sum = array.reduce((a, b) => {
              return a + b;
          }, 0);

expect(sum).toEqual(expected);

Upvotes: 1

Related Questions