Skip to content

Instantly share code, notes, and snippets.

@BideoWego
Last active December 1, 2017 20:59
Show Gist options
  • Save BideoWego/21d237388bb908e817aea882bcb1b870 to your computer and use it in GitHub Desktop.
Save BideoWego/21d237388bb908e817aea882bcb1b870 to your computer and use it in GitHub Desktop.
Ternary if, else if, else... chain
const fn = num => (
num >= 0 && num < 10 ? '1-9' :
num >= 10 && num < 20 ? '10-19' :
num >= 20 && num < 30 ? '20-29' :
num >= 30 && num < 40 ? '30-39' :
num >= 40 && num < 50 ? '40-49' :
num >= 50 && num < 60 ? '50-59' :
num >= 60 && num < 70 ? '60-69' :
num >= 70 && num < 80 ? '70-79' :
num >= 80 && num < 90 ? '80-89' :
num >= 90 && num < 100 ? '90-99' :
num >= 100 ? '+100' : '-'
);
const nums = [-1, 1, 11, 22, 33, 44, 55, 66, 77, 88, 99, 111];
nums.forEach(n => console.log(n, fn(n)));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment