Making decisions
Programs behave differently in different conditions. For example, some features of an app may only be available when a premium user is logged in. In this episode, we'll learn how to write programs that can make decisions.
Video
Exercises
This exercise is to help you get familiar with reading and understanding code.
Read the code below carefully and try to make sense of it, but don’t run it.
const newMessages = [ { from: 'Amal', body: 'When does it start?', time: '31 Dec 2021, 1:23 PM' }, { from: 'Isa', body: "I'm just going there now", time: '31 Dec 2021, 10:16 AM' }, ]; const messageCount = newMessages.length; let notification; if (messageCount === 0) { notification = "You have no new messages."; } else if (messageCount === 1) { notification = "You have a new message."; } else { notification = `You have ${messageCount} new messages.`; } console.log(notification);
What does the code do? What output do you expect? Observe that the
newMessages
array has two objects in it. If you removed one of the objects or both, or you added a new one, how would it affect the output of the code?Now run the code to see if it works as you expect. If you’re correct, then well done; you’re starting to get a hold of the language. If you’re not, don’t worry; it’s all part of the learning process; try to figure out where you went wrong.
Identify as many expressions and statements as you can in the code of the previous question. (You may need to review Values and types for this.)
Language codes are codes used to identify human languages. They’re often used in websites to organise content into different languages. An example of such a website is Wikipedia. If you visit a Wikipedia page, you might notice that the URL domain begins with a two-letter code, as in “en.wikipedia.org”. The “en” indicates that the page is in English. Similarly, an Arabic page would have the domain “ar.wikipedia.org”.
The following table lists some languages and their codes:
Language Code Arabic ar English en French fr Hausa ha Igbo ig Turkish tr Yoruba yo Complete the following program so that it prints the language that the
languageCode
variable corresponds to. The variable is currently set to"en"
, so the program should print English. If you change the value to a different language code from the above table, like"ha"
, the program should still work correctly.const languageCode = 'en'; // Your task: complete the program.
(Hint: use the
if
statement and its branches to decide what language to print.)
Extras
Loose equality
Many programming languages use the double equals symbol ==
to compare if two values are equal. We’ve seen that JavaScript uses the triple equals ===
instead, which we call the strict equality operator. However, JavaScript also supports the ==
operator for “loose” equality comparison.
The double equals operator works like the triple equals; it’s difference is that it converts the types of it’s operands, if they’re different, before comparing them. Let’s take an example:
// Strict equality
// Different types, different values
console.log(2 === '2') // false
// Loose equality
// The string will be converted to a number before comparing
console.log(2 == '2') // true
It’s discouraged to use this operator because of it’s confusing behavior:
console.log(false == null) // false
console.log(false == '') // true
console.log([1] == '1') // true
console.log([[]] == 0) // true
There’s a !=
counterpart for loose inequality too.
A shorter if
You may omit the braces in the if
statement if there’s only one statement within them. Take the following if
statement for example:
if (messageCount === 0) {
notification = "You have no new messages.";
}
There’s only one statement in the braces, so you may write it as:
if (messageCount === 0)
notification = "You have no new messages.";
If you have more than one statement, then you must use the braces:
if (messageCount === 0) {
notification = "You have no new messages.";
console.log(notification)
}
Omitting the braces otherwise changes the meaning of the program:
if (messageCount === 0)
notification = "You have no new messages.";
console.log(notification)
// The above is equivalent to:
if (messageCount === 0) {
notification = "You have no new messages.";
}
console.log(notification)
You may also omit the braces in the else if
and else branches
if they contain only one statement:
if (messageCount === 0)
notification = "You have no new messages.";
else if (messageCount === 1)
notification = "You have a new message.";
else
notification = `You have ${messageCount} new messages.`;
The braces actually create something called a block statement (“block” for short), which is just a way to group other statements.
Anyway, when you’re not sure, always use the braces.