Last active
April 21, 2023 18:07
-
-
Save gregdevs/3c430e47b02a77437165ebc40f8045a4 to your computer and use it in GitHub Desktop.
Brain Fart
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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