I'm struggling to formulate a Javascript function that would allow me to create 2 numbers out of a user input, according to 2 rules.
let userInput;
let num1;
let num2;
Rules: num1 + num2 = userInput and num1 - num2 must be the smallest positive number possible.
So with a user input of 5 the function should return 3 and 2 for num1 and num2 and not 4 and 1.
Could you please help me formulate such a Javascript function?
Thanks in advance for your help :)
Didn't get a notification when you responded. There are numerous ways to solve this, but I would use
Math.ceil(), that automatically rounds up any decimals, andMath.floor()that rounds down.Another way could be to divide
userInputby 2 and useparseInt()to cut off all decimals, and then adduserInput%2to that result, resulting in 1 ifuserInputis odd and 0 if even. Subtractnum1fromuserInputto getnum2.