Skip to content

Commit

Permalink
Merge pull request #22 from tlehwalder/birthday-cake-candles
Browse files Browse the repository at this point in the history
Adds test coverage for Birthday Cake Candles
  • Loading branch information
thamaraiselvam authored Oct 3, 2019
2 parents c3793c0 + 23b7cce commit 5a737d8
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 13 deletions.
24 changes: 11 additions & 13 deletions Arrays/birthday-cake-candles.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,17 @@
Link: https://www.hackerrank.com/challenges/birthday-cake-candles/
*/

function birthdayCakeCandles(arr) {
let tallestCandle = 0;
let candlesCounter = 0;
for (let index = 0; index < arr.length; index++) {
if (arr[index] > tallestCandle) {
tallestCandle = arr[index];
candlesCounter = 1;
} else if (arr[index] === tallestCandle) {
candlesCounter++;
}
module.exports = function birthdayCakeCandles(arr) {
let tallestCandle = 0;
let candlesCounter = 0;
for (let index = 0; index < arr.length; index++) {
if (arr[index] > tallestCandle) {
tallestCandle = arr[index];
candlesCounter = 1;
} else if (arr[index] === tallestCandle) {
candlesCounter++;
}
}

return candlesCounter;
return candlesCounter;
}

console.log(birthdayCakeCandles([3,2,1,3]));
28 changes: 28 additions & 0 deletions test/Arrays/birthday-cake-candles.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
const birthdayCakeCandles = require("../../Arrays/birthday-cake-candles");
const { describe, it } = require("mocha");
const { expect } = require("chai");

describe("Birthday Cake Candles", () => {
it("returns 0 for empty array", () => {
expect(birthdayCakeCandles([])).to.equal(0);
});

it("returns 1 if there is only one candle", () => {
expect(birthdayCakeCandles([1])).to.equal(1);
expect(birthdayCakeCandles([2])).to.equal(1);
expect(birthdayCakeCandles([3])).to.equal(1);
});

it("returns 2 if there are two candles with the same height", () => {
expect(birthdayCakeCandles([1, 1])).to.equal(2);
expect(birthdayCakeCandles([2, 2])).to.equal(2);
expect(birthdayCakeCandles([3, 3])).to.equal(2);
});

it("returns correct amount for array with different sized candles", () => {
expect(birthdayCakeCandles([3, 2, 1, 3])).to.equal(2);
expect(birthdayCakeCandles([3, 2, 1, 3, 4, 5, 4])).to.equal(1);
expect(birthdayCakeCandles([3, 2, 1, 1, 2, 2, 4, 4, 4])).to.equal(3);
expect(birthdayCakeCandles([3, 2, 1])).to.equal(1);
});
});

0 comments on commit 5a737d8

Please sign in to comment.