You are on page 1of 6

Discover the length

To discover the length of your name write your name within quotes. Then write a period (full
stop) and the word length like this:
"yourName".length

Editor and comments
The // sign is for comments. A comment is a line of text that JavaScript won't try to run as
code. It's just for humans to read and easily figure out what your code does.
The computer will ignore the code on lines 1-2, since it is commented out.
On line 3, find the length of the word "cake".

Interactive JavaScript
Examples:
confirm("I feel awesome!");
confirm("I am ready to go.");
These boxes can be used on websites to confirm things with users. You've probably seen
them pop up when you try to delete important things or leave a website with unsaved
changes.
Write your own message that you want the user to confirm.


What is programming?
Programming is like writing a list of instructions to the computer so it can do actions. To do
actions the program needs an input. You can ask for input with a prompt.
Examples:
1. prompt("What is your name?");
2. prompt("What is Ubuntu?");
Use the prompt command to ask the user where they are from. Check out the examples
above for how to do this!

Data Types I & II: Numbers & Strings
Data comes in various types.
a. numbers are quantities. You can do math with them.
b. strings are sequences of characters, like the letters a-z, spaces, and even numbers.
Strings are extremely useful as labels, names, and content for your programs.
To make a number in your code, just write a number as numerals without
quotes: 42, 190.12334.
To write a string, surround words with quotes: "What is your name?"
1. Write a string with at least 3 words. Check out the examples of strings above.




Data Type III: Booleans
A boolean is either true or false. For example, comparing two numbers returns
a true or false result:
23 > 10 is true
5 < 4 is false
Let's compare two numbers to return a true result:
1. First, write the string "I'm coding like a champ"
2. Next, find the length of the string using .length
3. Then, compare the string's length to see if it is greater than 10

Using console.log
console.log() will take whatever is inside the parentheses and log it to the console below
your code - that's why it's called console.log(). This is commonly called printing out.
Please print the following two console.log statements at the same time. Type one on line
1 and the other on line 2.
console.log(2 * 5)
console.log("Hello")

Comparisons
So far we've learned about three data types:
strings (e.g. "dogs go woof!")
numbers (e.g. 4, 10)
booleans (e.g. false, 5 > 4)
Now let's learn more about comparison operators.

List of comparison operators:
> Greater than
< Less than
<= Less than or equal to
>= Greater than or equal to
=== Equal to
!== Not equal to
1. Choose the correct comparison operator to make each of the four statements print
out true.
2. Write two more console.log statements that both evaluate to false.

Decisions, decisions
Now let's see how we can use comparisons to ask yes or no questions. Say we want to
write a program that asks whether your name is longer than 7 letters. If the answer is yes,
we can respond with "You have a long name!" We can do this with an if statement:
if( "myName".length >= 7 ) {
console.log("You have a long name!");
}
An if statement is made up of the if keyword, a condition like we've seen before, and a
pair of curly braces { }. If the answer to the condition is yes, the code inside the curly
braces will run.
1. On line 1, add a condition inside the parentheses ( ).
2. If the answer to the condition is yes, the code inside the curly braces will run. So
on line 2, use console.log to print out a string.

Computers are smart
In addition to doing something when the condition is true, we can do something else if the
condition is false. For example, if your name is shorter than 7 letters, we can respond with
"You have a short name!" We can do this using an if / else statement:
if( "myName".length >= 7 ) {
console.log("You have a long name!");
}
else {
console.log("You have a short name!");
}
Just like before, if the condition is true, then only the code inside the first pair of curly
braces will run. Otherwise, the condition is false, so only the code inside the second pair of
curly braces after the else keyword will run.
In the example above the condition "myName.length >= 7" evaluates to
false since "myName" only has 6 letters. Since the condition is false, only the code inside the
curly braces after the else keyword runs, and prints out You have a short name!.
1. In line 1, fill in a condition that will evaluate to false
2. Fill in some code to run in the else portion (this will run if the condition is false).
Use console.log for this part.

Write an if/else statement, just like we did in the last exercise. Here's what the outline of
the code looked like:
1. if (this condition is true)
2. {
3. // do this code
4. }
5. else // "otherwise"
6. {
7. // do this code instead
8. }
If your condition is true, use console.log to print "The condition is true".
Otherwise (else) when it is false, useconsole.log to print "The condition is false".
Make sure your condition evaluates to false, so that your program prints out "The condition
is false".

You might also like