Choices in JS

#teamtanayejschallenge- Part 3- Conditional Statements and Loops in JavaScript

Photo by James Harrison on Unsplash

Straight Line Flow Control

var name = prompt('What is your name?');
var greet = 'Good Morning, ' + name;
console.log(greet);
/* the above code snippet asks the user for the name, and greets Good Morning.*/

Conditional Statements

var name = prompt('What is your name?');if (timeOfDay == 'morning') {
greet = 'Good Morning! ' + name;
} else if (timeOfDay == 'evening') {
greet = 'Good Afternoon! ' + name;
} else {
greet = 'Hello! ' + name;
}
console.log(greet);/* this code will ask for the user's name, check the time of the day in the system and greet accordingly. */
if (condition) {
statement1;
} else if (condition) {
statement2;
} else {
statement2;
}
/* note:- 'else-if' part is not compulsory, if there are only 2 choices. Likewise, 'else' will be redundant if there is no choice for falsity. */
switch(case) {
case 1: statement1; break;
case 2: statement2; break;
case 3: statement3; break;
case 4: statement4; break;
default: statement;
}
/* the switch-case takes a variable and compare with the cases, if equal, executes the statement, otherwise skip. *// break essentially tells to come out of the block of code
switch(season) {
case 'Rainy':
case 'Winter':
case 'Spring': doNotEatIceCrean; break;
case 'Summer': iceCreamTime; break;
default: justTasteIceicream ;
}
/* easy enough to understand. If it is any season other than summer, do no eat ice-cream, you'll get sick. Summer it is? Enjoy your ice-cream. Don't know what season it is? You can have one scoop. */

Loopity loop

while(iLiveHere) {
supplyMilkTomorrow;
startNextDay;
}
/* as long as you are living in that flat, the milkman will knock everyday with the rise of the sun every morning. */
do {
supplyMilkTomorrow;
startNextDay;
} while (milkQuality == good)
/* if the quality goes down anytime, the purchase will be stopped from the next day. *//* also, note that you buy the first time without checking the quality, meaning you will get the milk, at least once. */
for(startMonth; livingHere; nextMonth) {
makePayment;
}
/* it seems tricky, but easy to understand going step by step. The first step is startMonth, that is the initialisation of the variable which will tell which month to start from to provide for milk. *//* the next code snippet in the bracket is livingHere, it is the condition for loop, as long as you are living there, the milk should be at your door *//*the last part is nextMonth, which is updating the month, so that we can make payment only when the next month arrive. */
// while-loopinitialisation;
while(condition) {
statement;
update expression;
}
// do-while loopinitialisation;
do {
statement;
update expression;
} while(condition)

// for loop
for(initialisation; condition; update expression;) {
statement;
}

Dreamer | Coder | Designer | Podcaster | Blogger

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store