Skip to content

Instantly share code, notes, and snippets.

@fazeelanizam13
Created May 10, 2022 15:53
Show Gist options
  • Save fazeelanizam13/30a55e590f19667f5f8e1c93dbe8594a to your computer and use it in GitHub Desktop.
Save fazeelanizam13/30a55e590f19667f5f8e1c93dbe8594a to your computer and use it in GitHub Desktop.
function swapWithoutThirdVariable({ firstNo, secondNo }) {
// if either of two input numbers don't pass as integers, return error
if (!Number.isInteger(firstNo) || !Number.isInteger(secondNo)) {
return 'Please input two integers for both "firstNo" and "secondNo"'
}
// first number is sum of two numbers
firstNo = firstNo + secondNo
// get first number again afer subtracting the second number from sum (firstNo)
secondNo = firstNo - secondNo
// get second number after subtracting the first number (secondNo) from sum (firstNo)
firstNo = firstNo - secondNo
return {
firstNoAfterSwap: firstNo,
secondNoAfterSwap: secondNo
}
}
console.log(swapWithoutThirdVariable({ firstNo: 10, secondNo: 2 }))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment