- We can give output to the screen with
console.log()
- Each call to
console.log()
puts a new line - We can put multiple items separated with
+
or,
- Each call to
- We can get input by using
prompt()
- We need to declare the prompt function at the top of our code
const prompt = require("prompt-sync")();
- We can store values in variables by using the
let
keyword- Variables can be reused without needing
let
again
- Variables can be reused without needing
You can jump ahead to the task, if you want.
One of the most used structures in programming is the if-else
statement. Every programming language has it (or something just like it).
It works like this:
if (some_sort_of_condition) {
// The code to run if the condition was true
}
This is how we make decisions like:
if (number_of_lives == 0) {
console.log("Game Over!");
}
- A single equal sign is the assignment operator:
x = 5
makesx
the number 5. - A double equal sign is the check operator to ask the question "Is it true"?
Operator | Meaning | Example |
---|---|---|
= | Assignment | let name = "Mr. Squirrel" |
== | Is Equal Returns true or false |
name == "Mr. Squirrel" |
!= | Is NOT Equal Returns true or false |
name != "Mr. Brash" |
> | Is Greater Returns true or false |
x > 10 |
>= | Is Greater or Equal Returns true or false |
x >= 10 |
< | Is Less Than Returns true or false |
Math.PI < 10 |
<= | Is Less Than or Equal Returns true or false |
Math.PI <= 3.15 |
-
Make a new file:
1.5 - Conditionals 1.js
-
We will be using
prompt()
so add the required line at the top of your code file. -
Ask the user for their age
- Don't forget, input is a String, not a number, so you'll have to convert it.
- if they are 50 years old or older, print to the screen "You qualify for the senior discount!"
- if they are younger than 16, print "You're not old enough to drive yet."
-
Now ask the user the user for their name and store the answer in a variable called
user_name
- if the
user_name
is "Mr. Squirrel" print the squirrel emoji: "🐿️" - if the
user_name
is longer than 7 characters print "You have a long name."- You can read how to get the length of a string here.
- if the
-
Now ask the user how long their name is (how many characters)
- If the number they enter matches the length of
user_name
print "That's correct! ✔️" - If the number they enter is higher than the length of the name, print "Too high ✖️"
- If the number they enter is lower than the length of the name, print "Too low ✖️"
- If the number they enter matches the length of
We've seen the basic math operators +
, -
, *
, /
but there are more. One important one is the modulo operator %
. This gives the remainder of a division statement.
For Example:
let x = 5 % 2
console.log(x) // The output will be 1
x = 19 % 5
console.log(x) // The output will be 4
x = 20 % 2
console.log(x) // The output will be 0
We can use modulo to determine if a value is even
or odd
by checking the remainder of dividing by 2.
- Determine if the user's age is
even
orodd
and print that to the screen.
For Example: "Your age is an even number." or "Your age is an odd number."
🐿️