Click to expand
How do you declare a variable. What does the equals sign really mean in JavaScript? What is it called in JavaScript?
Declare a variable using the var keyword followed by the variable name then a semicolon. var variableName;
Variables can be declared and assigned a value in the same line, var variableName = variableValue;
The equal sign is an assignment operator in JavaScript. It assigns a value to a variable. variableName = variableValue;
There are three big data types in JavaScript: numbers, strings, and booleans. Describe what each of them are.
Numeric data type consists of numbers they can be integers of decimal numbers.
String data type consists of text (letters, numbers and other characters). Although numbers can be included in a string the numbers would not be available for calculations with out operation performed on the string data type.
Boolean data type has two values, true and false.
What are the six rules for naming variables? What are a few JavaScript reserved words that you should avoid using for variable names?
- Variable names cannot start with a number. They can begin with a letter, dollar sign or underscore.
- Variable names cannot contain dashes or periods in their name. Letters, numbers, dollar signs, and underscores are acceptable.
- Variable names cannot be keywords or reserved words.
- Variable names are case sensitive.
- Use a name that describes what information the variable will contain.
- Use camel case when naming variables that have multi-word names.
Reserved words:
abstract, else, instanceof, super, boolean, enum, int, switch, break, export, interface, synchronized, byte, extends, let, this, case, false, long, throw, catch, final, native, throws, char, finally, new, transient, class, float, null, true, const, for, package, try, continue, function, private, typeof, debugger, goto, protected, var, default, if, public, void, delete, implements, return, volatile, do, import, short, while, double, in, static, with
How can an array be useful when dealing with multiple related values? How do you access/change a value in an array?
An array is a variable that can store multiple values. This is useful when dealing with a list or multiple related values by storing them all under one variable even if you do not know how many items will be included.
Accessing a specific value in an array is done by using the index number of the value. Index numbers start with 0 and count up until the last value in an array.
In the array stored in reservedWords:
var reservedWords
reservedWords = [‘abstract’,
‘else’,
‘super’,
‘boolean’];
Abstract has an index of 0, else has an index of 1, super has an index of 2, and boolean has an index of 3. Accessing the super value could be done by assigning a variable itemTwo
var reservedWords
var itemTwo
reservedWords = [‘abstract’,
‘else’,
‘super’,
‘boolean’];
itemTwo = reservedWords[2]
The value, super, would now be stored in the variable: itemTwo.
Changing a value in an array is done by using variable name, the index of the array value, and the assignment operator. The code reserveWords[2] = “false”
would replace the value of super with the value false.
What is the difference between an expression and a statement?
An expression produces a value and can be written wherever a value is expected. Expression have operators: assignment operators, arithmetic operators, string operators, comparison operators, and logical operators.
A statement performs an action. A program is a sequence of statements, forgoing declarations.
When JavaScript expects a statement, and expression can also be used. This is usually called and expression statement. A statement cannot be used where JavaScript expects an expression.
What are three types of operators and how are they used?
Three types of operators are: assignment operators, arithmetic operators, and string operators. There are two more operators: comparison operators and logical operators.
Operators create a single value from one ore more values.
The assignment operator places a value in a variable.
Arithmetic operators are used to perform basic math and store the new value in a variable.
String operators combine two or more strings together and store the new string in a variable.
For each task listed below, enter it in the console:
25
"this is my string" (notice the output's color difference between a number and a string)
var myNewString = "Hello Turing!";
myNewString Before you hit Enter, what do you expect to see in the console?
I expect the console to print "Hellow Turing!" with red letters and black quotes.
var myNum = 9;
myNum Before you hit Enter, what do you expect to see in the console?
I expect to see a blue 9.
var anotherString = "How are You?"
"Connect" + " " + "these" + " " + "strings." What happened? This is called string concatenation. Notice the strings with spaces.
myNewString + anotherString This is also string concatenation using variables.
5 > 3 returns a boolean value of true. How could you change this expression to return false?
Changing the greater than sign to a less than or swap the placement of the 5 and 3 would cause the expression to return a false.
"2" === 2 and "2" == 2 Why does one of those expressions return true and one return false?
In the case with the three equal signs, "2" === 2, JavaScript is checking to see is the items have the same type and value. They do not, the first is a string and the second is a number.
In the case with two equal signs, "2" == 2, JavaScript is checking for loose equality. Double equals causes JavaScript to performs type coercion. The comparison is made after an attempt to convert the values to a common type.
if (8 < 9) {console.log("Hey!")} Before you enter this code in the console, what do you think will happen? Will it log Hey to the console?
I believe that a black Hey! will appear in the console log. It did log Hey! in the console. It is even presistant when you close the dev tools and open them back up.
Use the console to solve these problems.
The Fortune Teller
Why pay a fortune teller when you can just program your fortune yourself?
Store the following into variables: number of children, partner's name, geographic location, job title.
Output your fortune to the screen like so: "You will be a X in Y, and married to Z with N kids."
var numKids = 4
undefined
var partnerName = "Jessica"
undefined
var geoLocation = "Denver, Colorado"
undefined
var jobTitle = "Software Developer"
undefined
"You will be a" + " " + jobTitle + " " + "in" + " " + geoLocation + ", and married to" + " " + partnerName + " with " + numKids + " kids."
"You will be a Software Developer in Denver, Colorado, and married to Jessica with 4 kids."
The Age Calculator
Forgot how old you are? Calculate it!
Store the current year in a variable.
Store your birth year in a variable.
Calculate your 2 possible ages based on the stored values.
Output them to the screen like so: "You are either NN or NN", substituting the values.
var currentYear = new Date().getFullYear()
undefined
var birthYear = 1976
undefined
var age = currentYear - birthYear
undefined
age
43
The Lifetime Supply Calculator
Ever wonder how much a "lifetime supply" of your favorite snack is? Wonder no more!
Store your current age into a variable.
Store a maximum age into a variable.
Store an estimated amount per day (as a number).
Calculate how many you would eat total for the rest of your life.
Output the result to the screen like so: "You will need NN to last you until the ripe old age of X".
var currentAge = 43
undefined
var maxAge = 96
undefined
var dailySnack = 3
undefined
var eaten = dailySnack * (maxAge - currentAge) * 365.25
undefined
"You will need " + eaten + " to last you intil the ripe old age of " + maxAge
"You will need 58074.75 to last you intil the ripe old age of 96"