Guest User

Untitled

a guest
May 27th, 2018
114
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.65 KB | None | 0 0
  1. // Given an array of positive integers, write a function which returns
  2. // all the unique pairs which add (equal) up to 100.
  3. const sampleData = [0, 1, 100, 99, 0, 10, 90, 30, 55, 33, 55, 75, 50, 51, 49, 50, 51, 49, 51];
  4. // sampleOutput = [[1,99], [0,100], [10,90], [51,49], [50,50]];
  5.  
  6. function pairs ( data ) {
  7. let ret = [];
  8. let check = [];
  9. data.forEach( i => {
  10. data.forEach( y => {
  11. if ( i + y === 100 ) {
  12. const one = `${i} ${y}`;
  13. const two = `${y} ${i}`;
  14.  
  15. if ( !check.includes( one ) && !check.includes( two ) ) {
  16. check.push( one );
  17.  
  18. ret.push( [ i, y ] );
  19. }
  20. }
  21. } );
  22. } );
  23.  
  24. return ret;
  25. }
Add Comment
Please, Sign In to add comment