Skip to content

Instantly share code, notes, and snippets.

@joefearnley
Last active June 27, 2020 14:24
Show Gist options
  • Save joefearnley/7ad8d4395f94875153a55dfdfc36f965 to your computer and use it in GitHub Desktop.
Save joefearnley/7ad8d4395f94875153a55dfdfc36f965 to your computer and use it in GitHub Desktop.
10 day JS challenge - Day 3: Alternating Sums
const alternatingSums = weights => [
weights.filter((weight, i) => i % 2 === 0).reduce((a, c) => a + c),
weights.filter((weight, i) => i % 2 !== 0).reduce((a, c) => a + c)
];
describe('alternatingSums()', () => {
it('returns alternating sums of even and odd', () => {
// arrange
const nums = [50, 60, 60, 45, 70];
// act
const result = alternatingSums(nums);
// log
console.log("result: ", result);
// assert
expect(result).toEqual([180, 105]);
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment