Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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]);
});
});