Need a unique gift idea?
A Pastebin account makes a great Christmas gift
SHARE
TWEET

Untitled

a guest May 27th, 2018 63 Never
Upgrade to PRO!
ENDING IN00days00hours00mins00secs
 
  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. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top