How To Use Conditionals
TypeScript is the programming language used in the Code Editors of DynaMaker.
Logic & Syntax
Conditionals work like if condition is true, do something:
if (condition) {
// do something
}
Optionally you can use multiple else-if to stack conditions, and a final else as fallback.
if (condition1) {
// do A
} else if (condition2) {
// do B
} else if (condition3) {
// do C
} else {
// do D
}
Conditions can be written like the following (most common ones):
-
numberconditions:===→ equal to (e.g.orcCount === 50)!==→ not equal to (e.g.orcCount !== 0)>→ larger than (e.g.orcCount > 10)<→ smaller than (e.g.orcCount < 100)>=→ larger than or equal to (e.g.orcCount >= 50)<=→ smaller than or equal to (e.g.orcCount <= 50)
-
stringconditions:===→ text is exactly the same (e.g.hero === "Gimli")!==→ text is different (e.g.hero !== "Legolas").includes()→ text contains something (e.g.hero.includes("im"))
-
booleanconditions:=== true→ check if value istrue(e.g.hasRing === true)=== false→ check if value isfalse(e.g.hasRing === false)if (condition)→ directly check if value istrue(e.g.if (hasRing))if (!condition)→ directly check if value isfalse(e.g.if (!hasRing))
-
list (
[]) conditions:.includes()→ list contains an item (e.g.party.includes("Gimli")).length→ check the number of items (e.g.party.length > 3)[index]→ access an item by its position (e.g.items[0] === "Sword")===→ check if it is the exact same list (e.g.party === anotherParty)
-
object (
{}) conditions:===→ check a property value (e.g.hero.name === "Aragorn")!==→ property is different (e.g.hero.name !== "Gimli")>→ property value is larger (e.g.hero.level > 5)<→ property value is smaller (e.g.hero.level < 5)in→ check if a property exists (e.g."name" in hero)JSON.stringify()→ convert object into text so it can be compared (e.g.JSON.stringify(hero) === JSON.stringify(otherHero))
-
undefinedor missing values:=== undefined→ check if something has no value (e.g.weapon === undefined)!== undefined→ check if something has a value (e.g.weapon !== undefined)if (value)→ check if a value exists (i.e. notundefined) and istrue(e.g.if (weapon))if (!value)→ check if a value is missing (i.e.undefined) ortrue(e.g.if (!weapon))
-
combining conditions:
&&→ AND: both conditions must betrue(e.g.orcCount > 10 && hasSword)||→ OR: at least one condition must betrue(e.g.hasSword || hasBow)!→ NOT: reversestrueandfalse(e.g.!hasRing)
Examples
Usually conditions are used to modify a variable or do some logic (e.g. add more extrusions to a model).
Note that the variables used below can change its value due to being defined as let, instead of the usual const,
whose value cannot change by definition.
Some examples of use:
-
single
if:let beaconCount = 7let message = 'The beacons are silent.'if (beaconCount === 7) {message = 'The beacons are lit! Gondor will answer!'}Example of use in function:
function getMessage(beaconCount: number) {let message = 'The beacons are silent.'if (beaconCount === 7) {message = 'The beacons are lit! Gondor will answer!'}return message} -
if+else:let orcCount = 1let report: stringif (orcCount >= 2) {report = 'That still only counts as one!'} else {report = 'One orc, a fine start!'}Example of use in function:
function getOrcReport(orcCount: number) {let report: stringif (orcCount >= 2) {report = 'That still only counts as one!'} else {report = 'One orc, a fine start!'}return report} -
if+else-if+else:let hasRing = truelet courage = 100let questStatus: stringif (hasRing && courage >= 100) {questStatus = 'The Ring-bearer begins the journey to Mount Doom'} else if (hasRing) {questStatus = 'The Ring is heavy. More courage is needed'} else {questStatus = 'Find the Ring before starting the quest'}Example of use in function:
function getQuestStatus(hasRing: boolean, courage: number) {let questStatus: stringif (hasRing && courage >= 100) {questStatus = 'The Ring-bearer begins the journey to Mount Doom'} else if (hasRing) {questStatus = 'The Ring is heavy. More courage is needed'} else {questStatus = 'Find the Ring before starting the quest'}return questStatus} -
multiple
else-ifwithoutquestStatusbut returning directly the output in afunction:function getQuestStatus(fellowshipMembers: number, hasRing: boolean) {if (hasRing && fellowshipMembers === 9) {return 'The Fellowship is formed. The journey begins! 💍'} else if (hasRing && fellowshipMembers >= 5) {return 'A brave company carries the Ring, but the Fellowship is incomplete.'} else if (hasRing) {return 'The Ring-bearer travels with only a few companions.'} else if (fellowshipMembers >= 9) {return 'The Fellowship is ready, but the Ring is missing!'} else {return 'The Council is deciding the fate of Middle-earth.'}} -
in-line
if(equivalent ofif+elsefor simpler conditions):const isElf = trueconst hero = isElf ? 'Legolas' : 'Not Legolas'Example of use in function:
function getHeroName(isElf: boolean) {const hero = isElf ? 'Legolas' : 'Not Legolas'return hero}