-
Notifications
You must be signed in to change notification settings - Fork 24
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #90 from rkalra247/probelm21
solution and test for problem #21
- Loading branch information
Showing
2 changed files
with
49 additions
and
0 deletions.
There are no files selected for viewing
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
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}; |
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
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]); | ||
}); | ||
}); |