Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- let inputArr = [40, 20, 30, 20, 10, 10, 40];
- let yourFunction = (arr, countMax) => {
- if (arr.length < countMax) throw new Error('Incorrect value for argument countMax');
- array = Array.from(new Set(arr)).sort( (a,b) => { return a - b });
- return array.slice(array.length - countMax);
- }
- let res1 = yourFunction(inputArr, 3); //res1 is [20, 30, 40];
- console.log(res1);
- try {
- // get 10 maximum values
- let res2 = yourFunction(inputArr, 10);
- console.log(res2);
- } catch (err) {
- // err is like “Incorrect value for argument countMax”
- console.error(err.message);
- }
Add Comment
Please, Sign In to add comment