findPairWithSum

In this example below you will see how to do a findPairWithSum with some HTML / CSS and Javascript

Thumbnail
This awesome code was written by vaskopetrov, you can see more from this user in the personal repository.
You can find the original code on Codepen.io
Copyright vaskopetrov ©
  • HTML
  • CSS
  • JavaScript
    

/*Downloaded from https://www.codeseek.co/vaskopetrov/findpairwithsum-YragNG */
    


/*Downloaded from https://www.codeseek.co/vaskopetrov/findpairwithsum-YragNG */
    var arr1 = [1,2,3,9];
var arr2 = [1,2,4,4];

function findPairWithSum(arr, sum) {
  var temp = [];
  for(var i = 0; i < arr.length; i++) {
    if(temp.indexOf(arr[i]) != -1) {
      var test = sum - temp[temp.indexOf(arr[i])];
      console.log('The numbers are: ' + test +' and ' + arr[i]);
      return true;
    }
    temp.push(sum-arr[i]);
  }
  console.log('The are no numbers matching to sum ' + sum);
  return false;
}

console.log(findPairWithSum(arr1, 8));

Comments