Last active
May 4, 2020 21:23
-
-
Save amykarnaze/b20adfa7586acf5b27389022bf7b9bce to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Your goal in this kata is to implement a difference function, which subtracts one list from another and returns the result. | |
It should remove all values from list a, which are present in list b. | |
Problem 1: | |
arrayDiff([1,2],[1]) == [2] | |
If a value is present in b, all of its occurrences must be removed from the other: | |
arrayDiff([1,2,2,2,3],[2]) == [1,3] | |
Solution: | |
Test.describe("Sample tests", function() { | |
Test.it("Should pass Sample tests", function() { | |
Test.assertDeepEquals(arrayDiff([], [4,5]), [], "a was [], b was [4,5]"); | |
Test.assertDeepEquals(arrayDiff([3,4], [3]), [4], "a was [3,4], b was [3]"); | |
Test.assertDeepEquals(arrayDiff([1,8,2], []), [1,8,2], "a was [1,8,2], b was []"); | |
}); | |
}); | |
function arrayDiff(a, b) { | |
var filteredArray = a.filter((number) => !b.includes(number)); | |
return filteredArray; | |
OR | |
function array_diff(a, b) { | |
return a.filter(function(value) | |
{ | |
return !(b.includes(value)) | |
}); | |
} | |
Problem 2: | |
// Sum Numbers | |
function sum (numbers) { | |
"use strict"; | |
var numberTotal= 0; | |
for(var i = 0; i < numbers.length; i++) { | |
numberTotal = numberTotal + numbers[i]; | |
} | |
return numberTotal; | |
}; | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment