4.1: Intro to Logic
Last updated
Last updated
By the end of this lesson, you should be able to do the following:
Know how logic is used to make programs more complex
Understand and use the if
statement.
Understand and use the equality operator: ==
So far our apps have always performed the same operations, no matter the input. The next level of complexity is to create programs that perform different operations, depending on the input.
From a programming perspective, logic is the ability of the computer to make decisions based on input data.
To begin with logic we'll be using the most basic JavaScript logic syntax, the if
condition. That means that some code will run or not depending on values we test. To start off with, those values will be what the user is typing in- so depending on what the user types, some different things will be displayed in the grey box.
Let's write a program that changes the output value of "hello world"
if we type in a particular phrase.
Our if statement is on line 5. The conditional inside it tests if input
is equal to 'palatable papaya'
, our secret phrase. If input
is equal to 'palatable papaya'
, the code runs between the curly braces on lines 5 and 7, i.e. the "if block". If input
is not equal to our phrase, the if block does not run.
Try inputting secret and non-secret phrases into the program. Enter the secret phrase and click the button to see the different output. Enter anything else and click the button to see the default output.
As our apps get more complicated, we can and should leave notes to ourselves and others to clarify what our code does. "Comments" let us write notes in our code files that are ignored on program execution. In JavaScript, comments are denoted by 2 slashes (//
) at the start of the comment. Every programming language has commenting functionality, though commenting syntax varies by language.
We will be using this 'Dice Rolling function' as a base to explore Logic and Control Flow for the rest of this Module.
The function we need is Math.random()
. Note the function call using ()
Calling Math.random()
returns a random decimal number between 0 and 1, inclusive of 0 and exclusive of 1.
Note that the Math.random()
function does not take in an input.
Since we wish to simulate dice with numbers between 1 to 6 inclusive, we have to manipulate the randomly-generated number to get what we want.
With Math.random()
and Math.floor()
, we can make a function that produces any random integer from 0 to a provided max
number:
Since we wish to build a program that generates random dice numbers. We use the logic from the above getRandomInteger
to always return an integer from 1 to 6. We call the new function rollDice
.
Using our new knowledge of 'logic' in this chapter, let's implement a simple rule to create a "dice game": if the user enters the same number as the dice roll, they win.
Duplicate and run the code above.
Update our dice game logic such that the user wins if the dice roll is 2 times the guess, e.g. a guess of 1 and roll of 2, a guess of 2 and roll of 4, etc. To win this updated game for a 6-sided dice, the user should only guess numbers between 1 and 3, but the game does not restrict what the user can guess.
An "if statement" is a control-flow "code block" that runs if a condition is true
. A code block is a section of code surrounded by curly braces. We'll talk more about what true
means when we introduce the boolean data type in .
Code blocks may or may not run depending on "", i.e. the logic of our app. The 1st way we learned to use code blocks was with functions. If statements are a 2nd way. We'll learn a 3rd code block syntax later in .
We're using the "" ==
to test if input
is equal to 'palatable papaya'
.
There are two comparison operators in JavaScript to check for equality, ==
, known as the abstract equality operator, and ===
, the strict equality operator. For the purpose of this course, ==
will suffice, but you are free to explore and experiment. You can read more about the in-depth differences between them in discussion.
Let's recap and build onto by building a function that generates random dice numbers.
To simulate dice, we first need random number generation. JavaScript can produce random numbers using a built-in "library" called (case-sensitive). Math
contains functions that perform common and helpful math operations.
To convert our random number to a valid dice roll value, we'll use another Math
function: Math.floor()
. We will follow the random integer generation example to use Math.floor()
to convert decimals to integers.