Skip to content

Instantly share code, notes, and snippets.

@gregdevs
Last active April 21, 2023 18:07
Show Gist options
  • Save gregdevs/3c430e47b02a77437165ebc40f8045a4 to your computer and use it in GitHub Desktop.
Save gregdevs/3c430e47b02a77437165ebc40f8045a4 to your computer and use it in GitHub Desktop.
Brain Fart
function checkIfTwoNumbersAddUp(list, x) {
let currentElement;
let currentPos = 0;
let next = 0;
let hasPair = false;
const loopThrough = () => {
if (list.length - 1 === currentPos) {
return;
}
if (currentElement + next === x) {
hasPair = true;
return;
} else {
currentPos++;
//Here is where I went wrong in my coderpad example
next = list[currentPos + 1];
loopThrough();
}
};
list.forEach((element, index) => {
currentPos = index;
currentElement = element;
next = list[index + 1];
loopThrough();
});
return hasPair;
}
console.log(checkIfTwoNumbersAddUp([1, 1, 2, 5, 4], 10));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment