Guest User

Untitled

a guest
Jan 19th, 2019
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.59 KB | None | 0 0
  1. let inputArr = [40, 20, 30, 20, 10, 10, 40];
  2.  
  3. let yourFunction = (arr, countMax) => {
  4.  
  5. if (arr.length < countMax) throw new Error('Incorrect value for argument countMax');
  6.  
  7. array = Array.from(new Set(arr)).sort( (a,b) => { return a - b });
  8.  
  9. return array.slice(array.length - countMax);
  10. }
  11.  
  12. let res1 = yourFunction(inputArr, 3); //res1 is [20, 30, 40];
  13. console.log(res1);
  14. try {
  15. // get 10 maximum values
  16. let res2 = yourFunction(inputArr, 10);
  17. console.log(res2);
  18. } catch (err) {
  19. // err is like “Incorrect value for argument countMax”
  20. console.error(err.message);
  21. }
Add Comment
Please, Sign In to add comment