Skip to content

Instantly share code, notes, and snippets.

@thykka
Created November 4, 2021 18:48
Show Gist options
  • Save thykka/133a0b83ad701c037168d94238ab8cca to your computer and use it in GitHub Desktop.
Save thykka/133a0b83ad701c037168d94238ab8cca to your computer and use it in GitHub Desktop.
// using for-loop structure to perform an action until a condition is satisfied
for(
// for-loop initialization:
// create a var which is a function that returns 0, 1 or 2, given an age
check = age =>
// check for underage
age < 18
? 0
// check for oldness. what would a golfed loop be without a nested ternary?
: age < 66
? 1
: 2,
// also create a var for the age check result
result = 0;
// for-loop condition:
// 0 or 1 keep the loop running, 2 to stop
result < 2;
// for-loop action:
result = check(
// using template literal syntax to call a function with 2 less chars
prompt`ikä`
), // using a comma to separate 2 expressions within a for-loop statement, as semicolons cannot be used here
alert(
// result doubles as an index for the alert messages
['lapsi','aikuinen','vanhus'][result]
)
) {
// the empty for-loop block
}
for(c=a=>a<18?0:a<66?1:2,r=0;r<2;r=c(prompt`ikä`),alert(['lapsi','aikuinen','vanhus'][r])){}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment