Skip to content

Commit

Permalink
Merge pull request #90 from rkalra247/probelm21
Browse files Browse the repository at this point in the history
solution and test for problem #21
  • Loading branch information
dsope05 authored Jun 13, 2017
2 parents 6c473d5 + ee0b8c2 commit e883ddf
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 0 deletions.
35 changes: 35 additions & 0 deletions solutions/21.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
// Rahul Kalra
// Return array of largest 3 values in descending order from a given array

const max = (array) =>{
let max = array[0];
for(let i = 0; i < array.length; i++){
if(max <= array[i+1]){
max = array[i+1];
}
}
return max;
};

const removeNum = (array, max) =>{
let newArray = [];
for(let i = 0; i < array.length; i++){
if(!(max === array[i])){
newArray.push(array[i]);
}
}
return newArray;
};

const solution = (array) =>{
let a = 0;
let newArray = [];
for(let i = 0; i < 3; i++){
a = max(array);;
array = removeNum(array, a);
newArray.push(a);
}
return [newArray[0], newArray[1], newArray[2]];
};

module.exports = {solution};
14 changes: 14 additions & 0 deletions test/21.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
let expect = require('chai').expect;
let solution = require('../solutions/21').solution;

describe('Return 3 top largest values of an array in a descending order', () =>{
it('should return 3 top largest values of an array', () =>{
expect(solution([8,1,4,2,12,6,7,19,2,9])).to.eql([19, 12, 9]);
});
});

describe('Return 3 top largest values of an array in a descending order', () =>{
it('should return 3 top largest values of an array', () =>{
expect(solution([-8,-3,-7,-1,-20,-2])).to.eql([-1,-2,-3]);
});
});

0 comments on commit e883ddf

Please sign in to comment.