Created
May 10, 2022 15:53
-
-
Save fazeelanizam13/30a55e590f19667f5f8e1c93dbe8594a to your computer and use it in GitHub Desktop.
This file contains 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 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