javascript

Ace your homework & exams now with Quizwiz!

right to left

Assignment always goes from

var sum = 10 + 10;

Change the 0 so that sum will equal 20. var sum = 10 + 0;

myVar ++;

Change the code to use the ++ operator on myVar. Hint var myVar = 87; myVar = myVar + 1;

if (val > 5) { result = "Bigger than 5"; } else{ result = "5 or Smaller"; }

Combine the if statements into a single if/else statement. if (val > 5) { result = "Bigger than 5"; } if (val <= 5) { result = "5 or Smaller"; }

myvar

It is common to initialize a variable to an initial value in the same line as it is declared. var myVar = 0; Creates a new variable called

global variable

It is possible to have both local and global variables with the same name. When you do this, the local variable takes precedence over the

different casing

It is possible to have multiple distinct variables with the same name but

Recursion

is the concept that a function can be expressed in terms of itself. To help understand this, start by thinking about the following task: multiply the first n elements of an array to create the product of those elements. Using a for loop, you could do this:

(==)

is the counterpart to the equality operator (==). However, unlike the equality operator, which attempts to convert both values being compared to a common type, the strict equality operator does not perform a type conversion.

(!==)

is the logical opposite of the strict equality operator. It means "Strictly Not Equal" and returns false where strict equality would return true and vice versa. Strict inequality will not convert data types.

(!=)

is the opposite of the equality operator. It means "Not Equal" and returns false where equality would return true and vice versa. Like the equality operator, the inequality operator will convert data types of values while comparing.

.pop()

is used to "pop" a value off of the end of an array. We can store this "popped off" value by assigning it to a variable. In other words, it removes the last element from an array and returns that element.

Booleans

may only be one of two values: true or false. They are basically little on-off switches, where true is "on" and false is "off." These two states are mutually exclusive.

par

meaning the average number of strokes a golfer is expected to make in order to sink the ball in a hole to complete the play. Depending on how far above or below par your strokes are, there is a different nickname.

[["Bulls", 23], ["White Sox", 45]]

multi-dimensional array.

camel case

multi-word variable names have the first word in lowercase and the first letter of each subsequent word is capitalized.

typeof

n JavaScript, you can determine the type of a variable or a value with the

/=

operator divides a variable by another number. myVar = myVar / 5;

*=

operator multiplies a variable by a number.

multi-dimensional array

You can also nest arrays within other arrays, like below:his is also called a

functionName();

You can call or invoke this function by using its name followed by parentheses, like this:

--

You can easily decrement or decrease a variable by one with the

++

You can easily increment or add one to a variable with the

.length

You can find the length of a String value by writing

/* */

You can make a multi-line comment beginning with and ending with

loop

You can run the same code multiple times by using a

var firstName = "Charles"; var thirdToLastLetter = firstName[firstName.length - 3]; // thirdToLastLetter is "l"

You can use the same principle we just used to retrieve the last character in a string to retrieve the Nth-to-last character.

var sandwich = ["peanut butter", "jelly", "bread"].

You start an array declaration with an opening square bracket, end it with a closing square bracket, and put a comma between each entry, like this:

a comma

You will need to place this after every object in the array, unless it is the last object in the array.

variables, label

allow computers to store and manipulate data in a dynamic fashion. They do this by using a "?" to point to the data rather than using the data itself. Any of the seven data types may be stored in a variable.

onditional operator

also called the ternary operator, can be used as a one line if-else expression. The syntax is: condition ? statement-if-true : statement-if-false;

Parameters

are variables that act as placeholders for the values that are to be input to a function when it is called

break is encountered.

f the break statement is omitted from a switch statement's case, the following case statement(s) are executed until af the break statement is omitted from a switch statement's case, the following case statement(s) are executed until a

A

for loop can also count backwards, so long as we can define the right conditions.

i = i + 1;

i++; is the equivalent of

nan

if you do a mathematical operation on an undefined variable your result will be

case sensitive

in JavaScript all variables and function names are

data

is a data type in JavaScript which represents numeric data.

queue

is an abstract Data Structure where items are kept in order. New items can be added at the back of the queue and old items are taken off from the front

"your name"

is called a string literal. It is a string because it is a series of zero or more characters enclosed in single or double quotes.

final-expression

is executed at the end of each loop iteration, prior to the next condition check and is usually used to increment or decrement your loop counter.

scope

refers to the visibility of variables. Variables which are defined outside of a function block have Global scope. This means, they can be seen everywhere in your JavaScript code.

array

we can store several pieces of data in one place.

flexible Data Structure

A JavaScript object is one way to handle flexible data. They allow for arbitrary combinations of strings, numbers, booleans, arrays, functions, and objects.

a for

A common task in JavaScript is to iterate through the contents of an array. One way to do that is with

var outerWear = "T-Shirt"; function myOutfit() { // Only change code below this line var myOutfit = "sweater"; return myOutfit;

Add a local variable to myOutfit function to override the value of outerWear with "sweater". var outerWear = "T-Shirt"; function myOutfit() { // Only change code below this line

function nextInLine(arr, item) { // Only change code below this line arr.push(item); return arr.shift(item);

Add the number to the end of the array, then remove the first element of the array. The nextInLine function should then return the element that was removed. function nextInLine(arr, item) { // Only change code below this line return item; // Only change code above this line

()

An easy way to append data to the end of an array is via the push fucntion

.pop()

Another way to change the data in an array is with the

camel case

Best Practice Write variable names in JavaScript in

string

Bracket notation is a way to get a character at a specific index within a

processed = processArg(7);

Call the processArg function with an argument of 7 and assign its return value to the variable processed. var processed = 0; function processArg(num) { return (num + 3) / 5; }

ignore

Comments are lines of code that JavaScript will intentionally

string

Computers can perform mathematical operations on a number, but not on a

function reusableFunction() { console.log("Hi World"); } reusableFunction()

Create a function called reusableFunction which prints "Hi World" to the dev console. Call the function.

function timesFive(num) { return num * 5; } var answer = timesFive(5);

Create a function timesFive that accepts one argument, multiplies it by 5, and returns the new value. See the last line in the editor for an example of how you can test your timesFive function. function timesFive(num) { return num * 5; } var answer = timesFive(5);

mylist

Create a shopping list in the variable myList. The list should be a multi-dimensional array containing several sub-arrays. The first element in each sub-array should contain a string with t

functions

In JavaScript, we can divide up our code into reusable parts called

concatenation operator

In JavaScript, when the + operator is used with a String value, it is called the

\ in front of the quote.

In JavaScript, you can escape a quote from considering it as an end of string quote by placing a backslash

undefined

If you concatenate a string with an undefined variable, you will get a literal string of

switch

If you have many options to choose from, this statement can be easier to write than many chained if/else if statements

switch statement.

If you have many options to choose from, use a

else if

If you have multiple conditions that need to be addressed, you can chain if statements together with

once created

In JavaScript, String values are immutable, which means that they cannot be altered

assignment operartor

In JavaScript, you can store a value in a variable with the

default

In a switch statement you may not be able to specify all possible values as case statements. Instead, you can add the

Type Coercion

In order for JavaScript to compare two different data types (for example, numbers and strings), it must convert one type to another. This is known as "

initialization condition final-expression.

In order to count backwards by twos, we'll need to change our

subtract one from the string's length.

In order to get the last letter of a string, you can

assignments

In programming, it is common to use what? to modify the contents of a variable

min and max

Instead of generating a random number between zero and a given number like we did before, we can generate a random number that falls within a range of two specific numbers. To do this, we'll define a minimum number

do...while loop

It is called a do...while loop because it will first do one pass of the code inside the loop no matter what, and then continue to run the loop while the specified condition evaluates to true.

declared

It is common to initialize a variable to an initial value in the same line as it is

undefined, null, boolean, string, symbol, number, and object.

JavaScript provides seven different data types which are

+

JavaScript uses the + symbol as an addition operator when placed between two numbers.

+=

Just as we can build a string over multiple lines out of string literals, we can also append variables to a string using the

function abTest(a, b) { // Only change code below this line if (a < 0||b < 0 ) { return undefined; }

Modify the function abTest so that if a or b are less than 0 the function will immediately exit with a value of undefined. Hint

0 known as zero based indexing

Most modern programming languages, like JavaScript, don't start counting at 1 like humans do. They start at

single or double quotes

String values in JavaScript may be written with?as long as you start and end with the same type of quote

properties

Objects are similar to arrays, except that instead of using indexes to access and modify their data, you access the data in objects through what are called

entries, next level entries

One way to think of a multi-dimensional array, is as an array of arrays. When you use brackets to access your array, the first set of brackets refers to the? in the outer-most (the first level) array, and each additional pair of brackets refers to the

first

Order is important in if, else if statements. The function is executed from top to bottom so you will want to be careful of what statement comes

f (val<= 50 && val >= 25) { return "Yes"; } return "No";

Replace the two if statements with one statement, using the && operator, which will return "Yes" if val is less than or equal to 50 and greater than or equal to 25. Otherwise, will return "No". if (val) { if (val) { return "Yes"; } }

(propname)

Sometimes it is useful to check if the property of a given object exists or not. We can use the .hasOwnProperty

+

Sometimes you will need to build a string, Mad Libs style. By using the concatenation operator

==

The equality operator compares two values and returns true if they're equivalent or false if they are not. Note that equality is different from assignment (=), which assigns the value on the right of the operator to a variable on the left.

==

The most basic operator is the equality operator

for loop

The most common type of JavaScript loop is called a

both in a string

The reason why you might want to use one type of quote over the other is if you want to use

%

The remainder operator % gives the remainder of the division of two numbers.

modulus

The remainder operator is sometimes incorrectly referred to as the "

([]).

The second way to access the properties of an object is bracket notation

dot or bracket notation.

The sub-properties of objects can be accessed by chaining together the

2

There are how many ways to write comments in JavaScript:

To allow you to use characters you may not otherwise be able to type out, such as a carriage return. To allow you to represent multiple quotes in a string without JavaScript misinterpreting what you mean.

There are two reasons to use escaping characters:

(.) ([])

There are two ways to access the properties of an object: dot notation

name and sqaure brakets

There shouldn't be any spaces between the array

changed

Unlike strings, the entries of arrays are mutable and can be

oopsGlobal =5; var myGlobal =10; function fun1() {

Using var, declare a global variable named myGlobal outside of any function. Initialize it with a value of 10. Inside function fun1, assign 5 to oopsGlobal without using the var keyword. function fun1() {

add them yourself

Watch out for spaces. Concatenation does not add spaces between concatenated strings, so you'll need to

indexes

We can access the data inside arrays using

/

We can also divide one number by another. JavaScript uses the

*

We can also multiply one number by another. JavaScript uses the

+=

We can also use this operator to concatenate a string onto the end of an existing string variable. This can be very helpful to break a long string over several lines.

return

We can pass values into a function with arguments. You can use a

floating point numbers or floats

We can store decimal numbers in variables too. Decimal numbers are sometimes referred to as

var

We tell JavaScript to create or declare a variable by putting the keyword

undefined

When JavaScript variables are declared, they have an initial value of

else

When a condition for an if statement is true, the block of code following it is executed. What about when that condition is false? Normally nothing would happen. With this statement

location

When a return statement is reached, the execution of the current function stops and control returns to the calling

will not execute

When the condition evaluates to true, the program executes the statement inside the curly braces. When the Boolean condition evaluates to false, the statement inside the curly braces

ojects

can be thought of as a key/value storage, like a dictionary. If you have tabular data, you can use an object to "lookup" values rather than a switch statement or an if/else chain.

Math.random()

can never quite return a 1 and, because we're rounding down, it's impossible to actually get 20. This technique will give us a whole number between 0 and 19.

(===).

case values are tested with strict equality

.shift()

comes in. It works just like .pop(), except it removes the first element instead of the last.

(>=)

compares the values of two numbers. If the number to the left is greater than or equal to the number to the right, it returns true. Otherwise, it returns false.

(>.)

compares the values of two numbers. If the number to the left is greater than the number to the right, it returns true. Otherwise, it returns false.

(<=)

compares the values of two numbers. If the number to the left is less than or equal to the number to the right, it returns true. If the number on the left is greater than the number on the right, it returns false. Like the equality operator, less than or equal to converts data types.

(<)

compares the values of two numbers. If the number to the left is less than the number to the right, it returns true. Otherwise, it returns false. Like the equality operator, less than operator converts data types while comparing.

parseInt()

function parses a string and returns an integer

parseInt()

function parses a string and returns an integer. It takes a second argument for the radix, which specifies the base of the number in the string. The radix can be an integer between 2 and 36.

Math.random()

function that generates a random decimal number between 0 (inclusive) and not quite up to 1 (exclusive). Thus Math.random() can return a 0 but never quite return a 1

function trueOrFalse(wasThatTrue) { // Only change code below this line if (wasThatTrue) { return "Yes, that was true";} {return "No, that was false";} test(true); // returns "Yes, that was true" test(false); // returns "No, that was false"

function trueOrFalse(wasThatTrue) { // Only change code below this line // Only change code above this line } Create an if statement inside the function to return "Yes, that was true" if the parameter wasThatTrue is true and return "No, that was false" otherwise.

(&&)

returns true if and only if the operands to the left and right of it are true.

(||)

returns true if either of the operands is true. Otherwise, it returns false.

condition

statement is evaluated at the beginning of every loop iteration and will continue as long as it evaluates to true. When condition is false at the start of the iteration, the loop will stop executing. This means if condition starts as false, your loop will never execute.

The initialization

statement is executed one time only before the loop starts. It is typically used to define and setup your loop variable.

if

statements are used to make decisions in code. The keyword if tells JavaScript to execute the code in the curly braces under certain conditions, defined in the parentheses. These conditions are known as Boolean conditions and they may only be true or false.

-=

subtracts a number from a variable.

plusthree

takes an argument for num and returns a value equal to num + 3.

push function

takes one or more parameters and "pushes" them onto the end of the array.

Booleans

values are never written with quotes. The strings "true" and "false" are not Boolean and have no special meaning in JavaScript.

//

will tell JavaScript to ignore the remainder of the text on the current line:

"The " + myAdjective + " " + myNoun + " " + myVerb + " " + myAdverb + " " + "." ;

wordBlanks should contain all of the words assigned to the variables myNoun, myVerb, myAdjective and myAdverb separated by non-word characters (and any additional words in your madlib). var wordBlanks = "";

.unshift()

works exactly like .push(), but instead of adding the element at the end of the array, this adds the element at the beginning of the array.


Related study sets

The most spoken languages in the world

View Set

vett 116 coursework - wk 5 exposure variables

View Set

Cumulative Exam Cumulative Exam Review

View Set

Medical Terminology Semester Exam

View Set

Civil War (Battles and Important People)

View Set

Chapter 5 - The Integumentary System - Review Questions

View Set