Created
May 5, 2023 10:53
-
-
Save amogower/4e48586da987224506ba3695858f95ac to your computer and use it in GitHub Desktop.
if vs switch
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 sendMoney(account, amount) { | |
if (account.balance < amount) { | |
console.log('Insufficient funds'); | |
return; | |
} | |
if (amount <= 0) { | |
console.log('Invalid transfer amount'); | |
return; | |
} | |
if (account.sender !== 'user-token') { | |
console.log('Forbidden user'); | |
return; | |
} | |
account.balance -= amount; | |
console.log('Transfer completed'); | |
} | |
------------------------------------------- | |
function sendMoney(account, amount) { | |
switch (true) { | |
case account.balance < amount: | |
console.log('Insufficient funds'); | |
return; | |
case amount <= 0: | |
console.log('Invalid transfer amount'); | |
return; | |
case account.sender !== 'user-token': | |
console.log('Forbidden user'); | |
return; | |
} | |
account.balance -= amount; | |
console.log('Transfer completed'); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment