Free Code Camp - Basic Javascript

Lakukan tugas rumah & ujian kamu dengan baik sekarang menggunakan Quizwiz!

Understanding Uninitialized Variables

When JavaScript variables are declared, they have an initial value of undefined. If you do a mathematical operation on an undefined variable your result will be NaN which means "Not a Number". If you concatenate a string with an undefined variable, you will get a literal string of "undefined". var a = 6; var b = 15; var c = "I am a String!";

Introducing Else Statements

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 an else statement, an alternate block of code can be executed. if (num > 10) { return "Bigger than 10"; } else { return "10 or Less"; } Ans: function testElse(val) { var result = ""; // Only change code below this line if (val > 5) { result = "Bigger than 5"; } else { result = "5 or Smaller"; } // Only change code above this line return result; } // Change this value to test testElse(4);

Return Early Pattern for Functions

When a return statement is reached, the execution of the current function stops and control returns to the calling location. Example function myFun() { console.log("Hello"); return "World"; console.log("byebye") } myFun(); The above outputs "Hello" to the console, returns "World", but "byebye" is never output, because the function exits at the return statement. Modify the function abTest so that if a or b are less than 0 the function will immediately exit with a value of undefined. Ans: // Setup function abTest(a, b) { // Only change code below this line if (a<0 || b<0){ return undefined; } // Only change code above this line return Math.round(Math.pow(Math.sqrt(a) + Math.sqrt(b), 2)); } // Change values below to test your code abTest(2,2);

Escaping Literal Quotes in Strings

When you are defining a string you must start and end with a single or double quote. What happens when you need a literal quote: " or ' inside of your string? In JavaScript, you can escape a quote from considering it as an end of string quote by placing a backslash (\) in front of the quote. var sampleStr = "Alan said, \"Peter is learning JavaScript\"."; This signals to JavaScript that the following quote is not the end of the string, but should instead appear inside the string. So if you were to print this to the console, you would get: Alan said, "Peter is learning JavaScript". Use backslashes to assign a string to the myStr variable so that if you were to print it to the console, you would see: I am a "double quoted" string inside "double quotes". var myStr = "I am a \"double quoted\" string inside \"double quotes\".";

Use Bracket Notation to Find the Nth-to-Last Character in a String

You can use the same principle we just used to retrieve the last character in a string to retrieve the Nth-to-last character. For example, you can get the value of the third-to-last letter of the var firstName = "Charles" string by using firstName[firstName.length - 3]

Count Backwards With a For Loop

A for loop can also count backwards, so long as we can define the right conditions. In order to count backwards by twos, we'll need to change our initialization, condition, and final-expression. We'll start at i = 10 and loop while i > 0. We'll decrement i by 2 each loop with i -= 2. var ourArray = []; for (var i=10; i > 0; i-=2) { ourArray.push(i); } ourArray will now contain [10,8,6,4,2]. Let's change our initialization and final-expression so we can count backward by twos by odd numbers. Ans: // Example var ourArray = []; for (var i = 10; i > 0; i -= 2) { ourArray.push(i); } // Setup var myArray = []; // Only change code below this line. for (var i=9; i > 0; i-=2) { myArray

Declare String Variables

Previously we have used the code var myName = "your name"; "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. var myFirstName = "Tom"; var myLastName = "Hall";

Comment Your JavaScript Code

// This is an in-line comment. /* This is a multi-line comment */

Iterate Through an Array with a For Loop

A common task in JavaScript is to iterate through the contents of an array. One way to do that is with a for loop. This code will output each element of the array arr to the console: var arr = [10,9,8,7,6]; for (var i = 0; i < arr.length; i++) { console.log(arr[i]); } Remember that Arrays have zero-based numbering, which means the last index of the array is length - 1. Our condition for this loop is i < arr.length, which stops when i is at length - 1. Ans: // Setup var myArr = [ 2, 3, 4, 5, 6]; // Only change code below this line var total = 0; for (var i = 0; i < myArr.length; i++) { total += myArr[i]; }

Concatenating Strings with the Plus Equals Operator

We can also use the += 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. var myStr = "This is the first sentence. " myStr += "This is the second sentence."

Understanding Undefined Value returned from a Function *

A function can include the return statement but it does not have to. In the case that the function doesn't have a return statement, when you call it, the function processes the inner code but the returned value is undefined. Example var sum = 0; function addSum(num) { sum = sum + num; } var returnedValue = addSum(3); // sum will be modified but returned value is undefined addSum is a function without a return statement. The function will change the global sum variable but the returned value of the function is undefined Ans: // Example var sum = 0; function addThree() { sum = sum + 3; } // Only change code below this line function addFive() { sum = sum + 5; } // Only change code above this line var returnedValue = addFive();

Updating Object Properties

After you've created a JavaScript object, you can update its properties at any time just like you would update any other variable. You can use either dot or bracket notation to update. For example, let's look at ourDog: var ourDog = { "name": "Camper", "legs": 4, "tails": 1, "friends": ["everything!"] }; Since he's a particularly happy dog, let's change his name to "Happy Camper". Here's how we update his object's name property: ourDog.name = "Happy Camper"; or ourDog["name"] = "Happy Camper"; Now when we evaluate ourDog.name, instead of getting "Camper", we'll get his new name, "Happy Camper". Ans: // Example var ourDog = { "name": "Camper", "legs": 4, "tails": 1, "friends": ["everything!"] }; ourDog.name = "Happy Camper"; // Setup var myDog = { "name": "Coder", "legs": 4, "tails": 1, "friends": ["freeCodeCamp Campers"] }; // Only change code below this line. myDog.name = "Happy Coder";

Manipulate Arrays With push( )

An easy way to append data to the end of an array is via the push() function. .push() takes one or more parameters and "pushes" them onto the end of the array. var arr = [1,2,3]; arr.push(4); // arr is now [1,2,3,4] // Setup var myArray = [["John", 23], ["cat", 2]]; // Only change code below this line. myArray.push(["dog", 3]);

Understanding Boolean Values

Another data type is the Boolean. 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. Note Boolean values are never written with quotes. The strings "true" and "false" are not Boolean and have no special meaning in JavaScript. Ans: function welcomeToBooleans() { // Only change code below this line. return true; // Change this line // Only change code above this line. }

Accessing Object Properties with Variables

Another use of bracket notation on objects is to access a property which is stored as the value of a variable. This can be very useful for iterating through an object's properties or when accessing a lookup table. Here is an example of using a variable to access a property: var dogs = { Fido: "Mutt", Hunter: "Doberman", Snoopie: "Beagle" }; var myDog = "Hunter"; var myBreed = dogs[myDog]; console.log(myBreed); // "Doberman" Another way you can use this concept is when the property's name is collected dynamically during the program execution, as follows: var someObj = { propName: "John" }; function propPrefix(str) { var s = "prop"; return s + str; } var someProp = propPrefix("Name"); // someProp now holds the value 'propName' console.log(someObj[someProp]); // "John" Note that we do not use quotes around the variable name when using it to access the property because we are using the value of the variable, not the name. Ans: // Setup var testObj = { 12: "Namath", 16: "Montana", 19: "Unitas" }; // Only change code below this line; var playerNumber = 16; var player = testObj[playerNumber];

Manipulate Arrays With pop( )

Another way to change the data in an array is with the .pop() function. .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, .pop() removes the last element from an array and returns that element. Any type of entry can be "popped" off of an array - numbers, strings, even nested arrays. var threeArr = [1, 4, 6]; var oneDown = threeArr.pop(); console.log(oneDown); // Returns 6 console.log(threeArr); // Returns [1, 4] // Setup var myArray = [["John", 23], ["cat", 2]]; // Only change code below this line. var removedFromMyArray = myArray.pop();

Accessing Nested Arrays

As we have seen in earlier examples, objects can contain both nested objects and nested arrays. Similar to accessing nested objects, Array bracket notation can be chained to access nested arrays. Here is an example of how to access a nested array: var ourPets = [ { animalType: "cat", names: [ "Meowzer", "Fluffy", "Kit-Cat" ] }, { animalType: "dog", names: [ "Spot", "Bowser", "Frankie" ] } ]; ourPets[0].names[1]; // "Fluffy" ourPets[1].names[0]; // "Spot" Ans: // Setup var myPlants = [ { type: "flowers", list: [ "rose", "tulip", "dandelion" ] }, { type: "trees", list: [ "fir", "pine", "birch" ] } ]; // Only change code below this line var secondTree = myPlants[1].list[1];

Storing Values with the Assignment Operator

Assignment always goes from right to left. Everything to the right of the = operator is resolved before the value is assigned to the variable to the left of the operator. myVar = 5; myNum = myVar; This assigns 5 to myVar and then resolves myVar to 5 again and assigns it to myNum.

Create Decimal Numbers with JavaScript

We can store decimal numbers in variables too. Decimal numbers are sometimes referred to as floating point numbers or floats. Note Not all real numbers can accurately be represented in floating point. This can lead to rounding errors. Details Here. var myDecimal = 6.5;

Use Bracket Notation to Find the First Character in a String

Bracket notation is a way to get a character at a specific index within a string. Most modern programming languages, like JavaScript, don't start counting at 1 like humans do. They start at 0. This is referred to as Zero-based indexing. For example, the character at index 0 in the word "Charles" is "C". So if var firstName = "Charles", you can get the value of the first letter of the string by using firstName[0]. // Setup var firstLetterOfLastName = ""; var lastName = "Lovelace"; // Only change code below this line firstLetterOfLastName = lastName[0];

Shopping List

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 the name of the item. The second element should be a number representing the quantity i.e. ["Chocolate Bar", 15] There should be at least 5 sub-arrays Ans: var myList = [["Bread", 4], ["Milk", 4], ["Banana", 8], ["Almonds", 6], ["Spinach", 10]];

Iterate Odd Numbers With a For Loop

For loops don't have to iterate one at a time. By changing our final-expression, we can count by even numbers. We'll start at i = 0 and loop while i < 10. We'll increment i by 2 each loop with i += 2. var ourArray = []; for (var i = 0; i < 10; i += 2) { ourArray.push(i); } ourArray will now contain [0,2,4,6,8]. Let's change our initialization so we can count by odd numbers. Ans: // Example var ourArray = []; for (var i = 0; i < 10; i += 2) { ourArray.push(i); } // Setup var myArray = []; // Only change code below this line. for (var i = 1; i < 11; i += 2) { myArray.push(i); }

Use Conditional Logic with If Statements

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. 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 will not execute. Ans: function trueOrFalse(wasThatTrue) { if (wasThatTrue) { return "Yes, that was true"; } return "No, that was false"; } trueOrFalse(true);

Multiple Identical Options in Switch Statements

If the break statement is omitted from a switch statement's case, the following case statement(s) are executed until a break is encountered. If you have multiple inputs with the same output, you can represent them in a switch statement like this: switch(val) { case 1: case 2: case 3: result = "1, 2, or 3"; break; case 4: result = "4 alone"; } Cases for 1, 2, and 3 will all produce the same result. Write a switch statement to set answer for the following ranges: 1-3 - "Low" 4-6 - "Mid" 7-9 - "High" Note Ans: function sequentialSizes(val) { var answer = ""; // Only change code below this line switch(val) { case 1: case 2: case 3: answer = "Low"; break; case 4: case 5: case 6: answer = "Mid"; break; case 7: case 8: case 9: answer = "High"; break; } // Only change code above this line return answer; } // Change this value to test sequentialSizes(1);

Nesting For Loops

If you have a multi-dimensional array, you can use the same logic as the prior waypoint to loop through both the array and any sub-arrays. Here is an example: var arr = [ [1,2], [3,4], [5,6] ]; for (var i=0; i < arr.length; i++) { for (var j=0; j < arr[i].length; j++) { console.log(arr[i][j]); } } This outputs each sub-element in arr one at a time. Note that for the inner loop, we are checking the .length of arr[i], since arr[i] is itself an array. Ans: function multiplyAll(arr) { var product = 1; // Only change code below this line for ( var i = 0; i < arr.length; i++) { for (var j = 0; j < arr[i].length; j++) { product *= arr[i][j]; } } return product; } multiplyAll([[1,2],[3,4],[5,6,7]]);

Replacing If Else Chains with Switch

If you have many options to choose from, a switch statement can be easier to write than many chained if/else if statements. The following: if (val === 1) { answer = "a"; } else if (val === 2) { answer = "b"; } else { answer = "c"; } can be replaced with: switch(val) { case 1: answer = "a"; break; case 2: answer = "b"; break; default: answer = "c"; } Change the chained if/else if statements into a switch statement. Ans: function chainToSwitch(val) { var answer = ""; // Only change code below this line switch(val) { case "bob": answer = "Marley"; break; case 42: answer = "The Answer"; break; case 1: answer = "There is no #1"; break; case 99: answer = "Missed me by this much!"; break; case 7: answer = "Ate Nine"; break; } // Only change code above this line return answer; } // Change this value to test chainToSwitch(7);

Write Reusable JavaScript with Functions

In JavaScript, we can divide up our code into reusable parts called functions. Here's an example of a function: function functionName() { console.log("Hello World"); } You can call or invoke this function by using its name followed by parentheses, like this: functionName(); Each time the function is called it will print out the message "Hello World" on the dev console. All of the code between the curly braces will be executed every time the function is called. Ans: function reusableFunction() { console.log("Hi World") } reusableFunction();

Selecting from Many Options with Switch Statements

If you have many options to choose from, use a switch statement. A switch statement tests a value and can have many case statements which define various possible values. Statements are executed from the first matched case value until a break is encountered. Here is a pseudocode example: switch(num) { case value1: statement1; break; case value2: statement2; break; ... case valueN: statementN; break; } case values are tested with strict equality (===). The break tells JavaScript to stop executing statements. If the break is omitted, the next statement will be executed. Ans: function caseInSwitch(val) { var answer = ""; // Only change code below this line switch (val) { case 1: answer = "alpha"; break; case 2: answer = "beta"; break; case 3: answer = "gamma"; break; case 4: answer = "delta"; break; } // Only change code above this line return answer; } // Change this value to test caseInSwitch(1);

Introducing Else If Statements

If you have multiple conditions that need to be addressed, you can chain if statements together with else if statements. if (num > 15) { return "Bigger than 15"; } else if (num < 5) { return "Smaller than 5"; } else { return "Between 5 and 15"; } Ans: function testElseIf(val) { if (val > 10) { return "Greater than 10"; } else if (val < 5) { return "Smaller than 5"; } else return "Between 5 and 10"; } // Change this value to test testElseIf(7);

Assignment with a Returned Value *

If you'll recall from our discussion of Storing Values with the Assignment Operator, everything to the right of the equal sign is resolved before the value is assigned. This means we can take the return value of a function and assign it to a variable. Assume we have pre-defined a function sum which adds two numbers together, then: ourSum = sum(5, 12); will call sum function, which returns a value of 17 and assigns it to ourSum variable. Ans: // Setup var processed = 0; function processArg(num) { return (num + 3) / 5; } // Only change code below this line processed = processArg(7);

Stand in Line

In Computer Science a 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 of the queue. Write a function nextInLine which takes an array (arr) and a number (item) as arguments. 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. Ans: function nextInLine(testArr, item) { // Your code here testArr.push(item); return testArr.shift(); // Change this line } // Test Setup var testArr = [1,2,3,4,5]; // Display Code console.log("Before: " + JSON.stringify(testArr)); console.log(nextInLine(testArr, 6)); // Modify this line to test console.log("After: " + JSON.stringify(testArr));

Understand String Immutability

In JavaScript, String values are immutable, which means that they cannot be altered once created. For example, the following code: var myStr = "Bob"; myStr[0] = "J"; cannot change the value of myStr to "Job", because the contents of myStr cannot be altered. Note that this does not mean that myStr cannot be changed, just that the individual characters of a string literal cannot be changed. The only way to change myStr would be to assign it with a new string, like this: var myStr = "Bob"; myStr = "Job"; var myStr = "Jello World"; // Only change code below this line myStr = "Hello World"; // Fix Me

Global Scope and Functions

In JavaScript, 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. Variables which are used without the var keyword are automatically created in the global scope. This can create unintended consequences elsewhere in your code or when running a function again. You should always declare your variables with var. Ans: // Declare your variable here var myGlobal = 10; function fun1() { // Assign 5 to oopsGlobal Here oopsGlobal = 5; } // Only change code above this line function fun2() { var output = ""; if (typeof myGlobal != "undefined") { output += "myGlobal: " + myGlobal; } if (typeof oopsGlobal != "undefined") { output += " oopsGlobal: " + oopsGlobal; } console.log(output); }

Concatenating Strings with Plus Operator

In JavaScript, when the + operator is used with a String value, it is called the concatenation operator. You can build a new string out of other strings by concatenating them together. Example 'My name is Alan,' + ' I concatenate.' Note Watch out for spaces. Concatenation does not add spaces between concatenated strings, so you'll need to add them yourself. var myStr = "This is the start. " + "This is the end."

Adding a Default Option in Switch Statements

In a switch statement you may not be able to specify all possible values as case statements. Instead, you can add the default statement which will be executed if no matching case statements are found. Think of it like the final else statement in an if/else chain. A default statement should be the last case. switch (num) { case value1: statement1; break; case value2: statement2; break; ... default: defaultStatement; break; } Ans: function switchOfStuff(val) { var answer = ""; // Only change code below this line switch (val) { case "a": answer = "apple"; break; case "b": answer = "bird"; break; case "c": answer = "cat"; break; default: answer = "stuff"; break; } // Only change code above this line return answer; } // Change this value to test switchOfStuff("a");

Declare JavaScript Variables

In computer science, data is anything that is meaningful to the computer. JavaScript provides seven different data types which are undefined, null, boolean, string, symbol, number, and object. Variables allow computers to store and manipulate data in a dynamic fashion. They do this by using a "label" to point to the data rather than using the data itself. Any of the seven data types may be stored in a variable. We tell JavaScript to create or declare a variable by putting the keyword var in front of it, like so: var ourName;

Use Bracket Notation to Find the Last Character in a String

In order to get the last letter of a string, you can subtract one from the string's length. For example, if var firstName = "Charles", you can get the value of the last letter of the string by using firstName[firstName.length - 1]. // Setup var lastName = "Lovelace"; // Only change code below this line. var lastLetterOfLastName = lastName[lastName.length -1];

Manipulate Arrays With unshift()

Not only can you shift elements off of the beginning of an array, you can also unshift elements to the beginning of an array i.e. add elements in front of the array. .unshift() works exactly like .push(), but instead of adding the element at the end of the array, unshift() adds the element at the beginning of the array. Ans: // Setup var myArray = [["John", 23], ["dog", 3]]; myArray.shift(); // Only change code below this line. myArray.unshift(["Paul",35]);

Compound Assignment With Augmented Addition

In programming, it is common to use assignments to modify the contents of a variable. Remember that everything to the right of the equals sign is evaluated first, so we can say: myVar = myVar + 5; to add 5 to myVar. Since this is such a common pattern, there are operators which do both a mathematical operation and assignment in one step. One such operator is the += operator. var myVar = 1; myVar += 5; console.log(myVar); // Returns 6 var a = 3; var b = 17; var c = 12; // Only modify code below this line a += 12; b += 9; c += 7;

Counting Cards

In the casino game Blackjack, a player can gain an advantage over the house by keeping track of the relative number of high and low cards remaining in the deck. This is called Card Counting. Having more high cards remaining in the deck favors the player. Each card is assigned a value according to the table below. When the count is positive, the player should bet high. When the count is zero or negative, the player should bet low. Count Change Cards +1 2, 3, 4, 5, 6 0 7, 8, 9 -1 10, 'J', 'Q', 'K', 'A' You will write a card counting function. It will receive a card parameter, which can be a number or a string, and increment or decrement the global count variable according to the card's value (see table). The function will then return a string with the current count and the string Bet if the count is positive, or Hold if the count is zero or negative. The current count and the player's decision (Bet or Hold) should be separated by a single space. Example Output -3 Hold 5 Bet Hint Do NOT reset count to 0 when value is 7, 8, or 9. Do NOT return an array. Do NOT include quotes (single or double) in the output. Ans: function cc(card) { // Only change code below this line switch(card){ case 2: case 3: case 4: case 5: case 6: count++; break; case 10: case "J": case "Q": case "K": case "A": count--; break; } if (count > 0){ return count + " Bet"; } else { return count + " Hold"; } // Only change code above this line }

Golf Code

In the game of golf each hole has a 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. Your function will be passed par and strokes arguments. Return the correct string according to this table which lists the strokes in order of priority; top (highest) to bottom (lowest): Strokes Return 1 "Hole-in-one!" <= par - 2 "Eagle" par - 1 "Birdie" par "Par" par + 1 "Bogey" par + 2 "Double Bogey" >= par + 3 "Go Home!" par and strokes will always be numeric and positive. We have added an array of all the names for your convenience. Ans: function golfScore(par, strokes) { // Only change code below this line if (strokes==1) { return "Hole-in-one!"; } else if (strokes<=par-2) { return "Eagle"; } else if (strokes==par-1) { return "Birdie"; } else if (strokes==par) { return "Par"; } else if (strokes==par+1) { return "Bogey"; } else if (strokes==par+2) { return "Double Bogey"; } else if (strokes>=par+3) { return "Go Home!"; } // Only change code above this line } // Change these values to test golfScore(5, 4);

Practice comparing different values

In the last two challenges, we learned about the equality operator (==) and the strict equality operator (===). Let's do a quick review and practice using these operators some more. If the values being compared are not of the same type, the equality operator will perform a type conversion, and then evaluate the values. However, the strict equalty operator will compare both the data type and value as-is, without converting one type to the other. Examples 3 == '3' // returns true because JavaScript performs type converstion from string to number 3 === '3' // returns false because the types are different and type conversion is not performed Note In JavaScript, you can determine the type of a variable or a value with the typeof operator, as follows: typeof 3 // returns 'number' typeof '3' // returns 'string' Ans: function compareEquality(a, b) { if (a === b) { // Change this line return "Equal"; } return "Not Equal"; } compareEquality(10, "10");

Initializing Variables with the Assignment Operator

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 myVar and assigns it an initial value of 0.

Global vs. Local Scope in Functions

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 global variable. In this example: var someVar = "Hat"; function myFun() { var someVar = "Head"; return someVar; } The function myFun will return "Head" because the local version of the variable is present. Ans: // Setup var outerWear = "T-Shirt"; function myOutfit() { // Only change code below this line var outerWear = 'sweater'; // Only change code above this line return outerWear; } myOutfit();

Add Two Numbers with JavaScript

Number is a data type in JavaScript which represents numeric data. Now let's try to add two numbers using JavaScript. JavaScript uses the + symbol as addition operation when placed between two numbers. var sum = 10 + 10;

Understanding Case Sensitivity in Variables

MYVAR is not the same as MyVar nor myvar. It is possible to have multiple distinct variables with the same name but different casing. It is strongly recommended that for the sake of clarity, you do not use this language feature. var studlyCapVar = 10; var properCamelCase = "A String"; var titleCaseOver = 9000;

Using Objects for Lookups

Objects 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. This is most useful when you know that your input data is limited to a certain range. Here is an example of a simple reverse alphabet lookup: var alpha = { 1:"Z", 2:"Y", 3:"X", 4:"W", ... 24:"C", 25:"B", 26:"A" }; alpha[2]; // "Y" alpha[24]; // "C" var value = 2; alpha[value]; // "Y" Ans: // Setup function phoneticLookup(val) { var result = ""; // Only change code below this line var lookup= { "alpha": "Adams", "bravo": "Boston", "charlie":"Chicago", "delta":"Denver", "echo":"Easy", "foxtrot" : "Frank" } result=lookup[val]; // Only change code above this line return result; } // Change this value to test phoneticLookup("charlie");

Access Multi-Dimensional Arrays With Indexes *

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 entries in the outer-most (the first level) array, and each additional pair of brackets refers to the next level of entries inside. Example var arr = [ [1,2,3], [4,5,6], [7,8,9], [[10,11,12], 13, 14] ]; arr[3]; // equals [[10,11,12], 13, 14] arr[3][0]; // equals [10,11,12] arr[3][0][1]; // equals 11 Note There shouldn't be any spaces between the array name and the square brackets, like array [0][0] and even this array [0] [0] is not allowed. Although JavaScript is able to process this correctly, this may confuse other programmers reading your code. Ans: // Setup var myArray = [[1,2,3], [4,5,6], [7,8,9], [[10,11,12], 13, 14]]; // Only change code below this line. var myData = myArray[2][1];

Logical Order in If Else Statements

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 first. Take these two functions as an example. Here's the first: function foo(x) { if (x < 1) { return "Less than one"; } else if (x < 2) { return "Less than two"; } else { return "Greater than or equal to two"; } } And the second just switches the order of the statements: function bar(x) { if (x < 2) { return "Less than two"; } else if (x < 1) { return "Less than one"; } else { return "Greater than or equal to two"; } } While these two functions look nearly identical if we pass a number to both we get different outputs. foo(0) // "Less than one" bar(0) // "Less than two" Ans: function orderMyLogic(val) { if (val >= 10) { return "Greater than or equal to 10"; } else if (val < 10 && val > 5) { return "Less than 10"; } else { return "Less than 5"; } } // Change this value to test orderMyLogic(7);

Passing Values to Functions with Arguments

Parameters are variables that act as placeholders for the values that are to be input to a function when it is called. When a function is defined, it is typically defined along with one or more parameters. The actual values that are input (or "passed") into a function when it is called are known as arguments. Here is a function with two parameters, param1 and param2: function testFun(param1, param2) { console.log(param1, param2); } Then we can call testFun: testFun("Hello", "World"); We have passed two arguments, "Hello" and "World". Inside the function, param1 will equal "Hello" and param2 will equal "World". Note that you could call testFun again with different arguments and the parameters would take on the value of the new arguments. Ans: function functionWithArgs(a, b) { console.log(a + b); } functionWithArgs(7, 9);

Escape Sequences in Strings

Quotes are not the only characters that can be escaped inside a string. There are two reasons to use escaping characters: First is to allow you to use characters you might not otherwise be able to type out, such as a backspace. Second is to allow you to represent multiple quotes in a string without JavaScript misinterpreting what you mean. We learned this in the previous challenge. Code Output \' single quote \" double quote \\ backslash \n newline \r carriage return \t tab \b backspace \f form feed Note that the backslash itself must be escaped in order to display as a backslash. FirstLine \SecondLine ThirdLine var myStr = "\"FirstLine\n\t\\SecondLine\nThirdLine\"";

Testing Objects for Properties

Sometimes it is useful to check if the property of a given object exists or not. We can use the .hasOwnProperty(propname) method of objects to determine if that object has the given property name. .hasOwnProperty() returns true or false if the property is found or not. Example var myObj = { top: "hat", bottom: "pants" }; myObj.hasOwnProperty("top"); // true myObj.hasOwnProperty("middle"); // false Ans: // Setup var myObj = { gift: "pony", pet: "kitten", bed: "sleigh" }; function checkObj(checkProp) { // Your Code Here var PC = myObj[checkProp]; return PC || "Not Found"; return "Change Me!"; } // Test your code by modifying these values checkObj("gift");

Manipulating Complex Objects

Sometimes you may want to store data in a 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. Here's an example of a complex data structure: var ourMusic = [ { "artist": "Daft Punk", "title": "Homework", "release_year": 1997, "formats": [ "CD", "Cassette", "LP" ], "gold": true } ]; This is an array which contains one object inside. The object has various pieces of metadata about an album. It also has a nested "formats" array. If you want to add more album records, you can do this by adding records to the top level array. Objects hold data in a property, which has a key-value format. In the example above, "artist": "Daft Punk" is a property that has a key of "artist" and a value of "Daft Punk". JavaScript Object Notation or JSON is a related data interchange format used to store data. { "artist": "Daft Punk", "title": "Homework", "release_year": 1997, "formats": [ "CD", "Cassette", "LP" ], "gold": true } Note You will need to place a comma after every object in the array, unless it is the last object in the array. Ans: var myMusic = [ { "artist": "Billy Joel", "title": "Piano Man", "release_year": 1973, "formats": [ "CD", "8T", "LP" ], "gold": true }, // Add record here { "artist": "James Blunt", "title": "Record", "release_year": 1995, "formats": [ "DVD", "Record", "Ipod" ], } ];

Constructing Strings with Variables

Sometimes you will need to build a string, Mad Libs style. By using the concatenation operator (+), you can insert one or more variables into a string you're building. var myName = "Tom"; var myStr = "My name is " + myName + " and I am well"

Comparisons with the Logical And Operator

Sometimes you will need to test more than one thing at a time. The logical and operator (&&) returns true if and only if the operands to the left and right of it are true. The same effect could be achieved by nesting an if statement inside another if: if (num > 5) { if (num < 10) { return "Yes"; } } return "No"; will only return "Yes" if num is greater than 5 and less than 10. The same logic can be written as: if (num > 5 && num < 10) { return "Yes"; } return "No"; Ans: function testLogicalAnd(val) { // Only change code below this line if (val <= 50 && val >= 25) { return "Yes"; } // Only change code above this line return "No"; } // Change this value to test testLogicalAnd(10);

Comparison with the Strict Equality Operator

Strict equality (===) 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. If the values being compared have different types, they are considered unequal, and the strict equality operator will return false. Examples 3 === 3 // true 3 === '3' // false In the second example, 3 is a Number type and '3' is a String type. Ans: function testStrict(val) { if (val === 7) { // Change this line return "Equal"; } return "Not Equal"; } testStrict(10);

Quoting Strings with Single Quotes *

String values in JavaScript may be written with single or double quotes, as long as you start and end with the same type of quote. Unlike some other programming languages, single and double quotes work the same in JavaScript. doubleQuoteStr = "This is a string"; singleQuoteStr = 'This is also a string'; The reason why you might want to use one type of quote over the other is if you want to use both in a string. This might happen if you want to save a conversation in a string and have the conversation in quotes. Another use for it would be saving an <a> tag with various attributes in quotes, all within a string. conversation = 'Finn exclaims to Jake, "Algebraic!"'; However, this becomes a problem if you need to use the outermost quotes within it. Remember, a string has the same kind of quote at the beginning and end. But if you have that same quote somewhere in the middle, the string will stop early and throw an error. goodStr = 'Jake asks Finn, "Hey, let\'s go on an adventure?"'; badStr = 'Finn responds, "Let's go!"'; // Throws an error In the goodStr above, you can use both quotes safely by using the backslash \ as an escape character. Note The backslash \ should not be be confused with the forward slash /. They do not do the same thing. var myStr = '<a href="http://www.example.com" target="_blank">Link</a>';

Golf Code

T

Comparison with the Greater Than Operator

The greater than operator (>) 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. Like the equality operator, greater than operator will convert data types of values while comparing. Examples 5 > 3 // true 7 > '3' // true 2 > 3 // false '1' > 9 // false Ans: function testGreaterThan(val) { if (val > 100) { // Change this line return "Over 100"; } if (val > 10) { // Change this line return "Over 10"; } return "10 or Under"; } // Change this value to test testGreaterThan(10);

Comparison with the Greater Than Or Equal To Operator

The greater than or equal to operator (>=) 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. Like the equality operator, greater than or equal to operator will convert data types while comparing. Examples 6 >= 6 // true 7 >= '3' // true 2 >= 3 // false '7' >= 9 // false Ans: function testGreaterOrEqual(val) { if (val >= 20) { // Change this line return "20 or Over"; } if (val >= 10) { // Change this line return "10 or Over"; } return "Less than 10"; } // Change this value to test testGreaterOrEqual(10);

Comparison with the Inequality Operator

The inequality operator (!=) 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. Ans: function testNotEqual(val) { if (val != 99) { // Change this line return "Not Equal"; } return "Equal"; } testNotEqual(10);

Comparison with the Less Than Operator

The less than operator (<) 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. Examples 2 < 5 // true '3' < 7 // true 5 < 5 // false 3 < 2 // false '8' < 4 // false Ans: function testLessThan(val) { if (val < 25) { // Change this line return "Under 25"; } if (val < 55) { // Change this line return "Under 55"; } return "55 or Over"; } // Change this value to test testLessThan(10);

Comparison with the Less Than Or Equal To Operator

The less than or equal to operator (<=) 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. Examples 4 <= 5 // true '7' <= 7 // true 5 <= 5 // true 3 <= 2 // false '8' <= 4 // false Ans: function testLessOrEqual(val) { if (val <= 12) { // Change this line return "Smaller Than or Equal to 12"; } if (val <= 24) { // Change this line return "Smaller Than or Equal to 24"; } return "More Than 24"; } // Change this value to test testLessOrEqual(10);

Comparisons with the Logical Or Operator

The logical or operator (||) returns true if either of the operands is true. Otherwise, it returns false. The logical or operator is composed of two pipe symbols (|). This can typically be found between your Backspace and Enter keys. The pattern below should look familiar from prior waypoints: if (num > 10) { return "No"; } if (num < 5) { return "No"; } return "Yes"; will return "Yes" only if num is between 5 and 10 (5 and 10 included). The same logic can be written as: if (num > 10 || num < 5) { return "No"; } return "Yes"; Ans: function testLogicalOr(val) { // Only change code below this line if (val > 20 || val < 10) { return "Outside"; } // Only change code above this line return "Inside"; } // Change this value to test testLogicalOr(15);

Return a Value from a Function with Return

We can pass values into a function with arguments. You can use a return statement to send a value back out of a function. Example function plusThree(num) { return num + 3; } var answer = plusThree(5); // 8 plusThree takes an argument for num and returns a value equal to num + 3. Ans: // Example function minusSeven(num) { return num - 7; } // Only change code below this line function timesFive(num) { return num * 5; } console.log(minusSeven(10)); console.log(timesFive(5));

Accessing Object Properties with Bracket Notation *

The second way to access the properties of an object is bracket notation ([]). If the property of the object you are trying to access has a space in its name, you will need to use bracket notation. However, you can still use bracket notation on object properties without spaces. Here is a sample of using bracket notation to read an object's property: var myObj = { "Space Name": "Kirk", "More Space": "Spock", "NoSpace": "USS Enterprise" }; myObj["Space Name"]; // Kirk myObj['More Space']; // Spock myObj["NoSpace"]; // USS Enterprise Note that property names with spaces in them must be in quotes (single or double). Read the values of the properties "an entree" and "the drink" of testObj using bracket notation and assign them to entreeValue and drinkValue respectively. Ans: // Setup var testObj = { "an entree": "hamburger", "my side": "veggies", "the drink": "water" }; // Only change code below this line var entreeValue = testObj["an entree"]; // Change this line var drinkValue = testObj["the drink"]; // Change this line

Comparison with the Strict Inequality Operator

The strict inequality operator (!==) 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. Examples 3 !== 3 // false 3 !== '3' // true 4 !== 3 // true Ans: // Setup function testStrictNotEqual(val) { // Only Change Code Below this Line if (val !== 17) { // Only Change Code Above this Line return "Not Equal"; } return "Equal"; } // Change this value to test testStrictNotEqual(10);

Accessing Nested Objects

The sub-properties of objects can be accessed by chaining together the dot or bracket notation. Here is a nested object: var ourStorage = { "desk": { "drawer": "stapler" }, "cabinet": { "top drawer": { "folder1": "a file", "folder2": "secrets" }, "bottom drawer": "soda" } }; ourStorage.cabinet["top drawer"].folder2; // "secrets" ourStorage.desk.drawer; // "stapler" Ans: // Setup var myStorage = { "car": { "inside": { "glove box": "maps", "passenger seat": "crumbs" }, "outside": { "trunk": "jack" } } }; var gloveBoxContents = myStorage.car.inside["glove box"] // Change this line

Comparison with the Equality Operator

There are many Comparison Operators in JavaScript. All of these operators return a boolean true or false value. The most basic operator is the equality 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 at the right of the operator to a variable in the left. function equalityTest(myVal) { if (myVal == 10) { return "Equal"; } return "Not Equal"; } If myVal is equal to 10, the equality operator returns true, so the code in the curly braces will execute, and the function will return "Equal". Otherwise, the function will return "Not Equal". 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 "Type Coercion". Once it does, however, it can compare terms as follows: 1 == 1 // true 1 == 2 // false 1 == '1' // true "3" == 3 // true Ans: // Setup function testEqual(val) { if (val == 12) { // Change this line return "Equal"; } return "Not Equal"; } // Change this

Accessing Object Properties with Dot Notation

There are two ways to access the properties of an object: dot notation (.) and bracket notation ([]), similar to an array. Dot notation is what you use when you know the name of the property you're trying to access ahead of time. Here is a sample of using dot notation (.) to read an object's property: var myObj = { prop1: "val1", prop2: "val2" }; var prop1val = myObj.prop1; // val1 var prop2val = myObj.prop2; // val2 Read in the property values of testObj using dot notation. Set the variable hatValue equal to the object's property hat and set the variable shirtValue equal to the object's property shirt. Ans: // Setup var testObj = { "hat": "ballcap", "shirt": "jersey", "shoes": "cleats" }; // Only change code below this line var hatValue = testObj.hat; // Change this line var shirtValue = testObj.shirt; // Change this line

Modify Array Data With Indexes

Unlike strings, the entries of arrays are mutable and can be changed freely. Example var ourArray = [50,40,30]; ourArray[0] = 15; // equals [15,40,30] Note There shouldn't be any spaces between the array name and the square brackets, like array [0]. Although JavaScript is able to process this correctly, this may confuse other programmers reading your code. // Example var ourArray = [18,64,99]; ourArray[1] = 45; // ourArray now equals [18,45,99]. // Setup var myArray = [18,64,99]; // Only change code below this line. myArray[0] = 45

Finding a Remainder in JavaScript

Usage In mathematics, a number can be checked to be even or odd by checking the remainder of the division of the number by 2. 17 % 2 = 1 (17 is Odd) 48 % 2 = 0 (48 is Even) var remainder = 11 % 3;

Local Scope and Functions

Variables which are declared within a function, as well as the function parameters have local scope. That means, they are only visible within that function. Here is a function myTest with a local variable called loc. function myTest() { var loc = "foo"; console.log(loc); } myTest(); // logs "foo" console.log(loc); // loc is not defined loc is not defined outside of the function. Ans:

Access Array Data with Indexes *

We can access the data inside arrays using indexes. Array indexes are written in the same bracket notation that strings use, except that instead of specifying a character, they are specifying an entry in the array. Like strings, arrays use zero-based indexing, so the first element in an array is element 0. var myData = myArray[0]

Delete Properties from a JavaScript Object

We can also delete properties from objects like this: delete ourDog.bark; Ans: // Setup var myDog = { "name": "Happy Coder", "legs": 4, "tails": 1, "friends": ["freeCodeCamp Campers"], "bark": "woof" }; // Only change code below this line. delete myDog.tails;

Word Blanks

We will now use our knowledge of strings to build a "Mad Libs" style word game we're calling "Word Blanks". You will create an (optionally humorous) "Fill in the Blanks" style sentence. In a "Mad Libs" game, you are provided sentences with some missing words, like nouns, verbs, adjectives and adverbs. You then fill in the missing pieces with words of your choice in a way that the completed sentence makes sense. Consider this sentence - "It was really ____, and we ____ ourselves ____". This sentence has three missing pieces- an adjective, a verb and an adverb, and we can add words of our choice to complete it. We can then assign the completed sentence to a variable as follows: var sentence = "It was really" + "hot" + ", and we" + "laughed" + "ourselves" + "silly."; In this challenge, we provide you with a noun, a verb, an adjective and an adverb. You need to form a complete sentence using words of your choice, along with the words we provide. You will need to use the string concatenation operator + to build a new string, using the provided variables: myNoun, myAdjective, myVerb, and myAdverb. You will then assign the formed string to the result variable. You will also need to account for spaces in your string, so that the final sentence has spaces between all the words. The result should be a complete sentence. Ans: function wordBlanks(myNoun, myAdjective, myVerb, myAdverb) { // Your code below this line var result = myAdjective+ " "+myNoun + " " + " " + myVerb + " " + myAdverb+"!"; return result; } // Change the words here to test your function wordBlanks("dog", "big", "ran", "quickly");

Store Multiple Values in one Variable using JavaScript Arrays

With JavaScript array variables, we can store several pieces of data in one place. 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: var sandwich = ["peanut butter", "jelly", "bread"]. var myArray = ["Tom", 24];

Record Collection *

You are given a JSON object representing a part of your musical album collection. Each album has several properties and a unique id number as its key. Not all albums have complete information. Write a function which takes an album's id (like 2548), a property prop (like "artist" or "tracks"), and a value (like "Addicted to Love") to modify the data in this collection. If prop isn't "tracks" and value isn't empty (""), update or set the value for that record album's property. Your function must always return the entire collection object. There are several rules for handling incomplete data: If prop is "tracks" but the album doesn't have a "tracks" property, create an empty array before adding the new value to the album's corresponding property. If prop is "tracks" and value isn't empty (""), push the value onto the end of the album's existing tracks array. If value is empty (""), delete the given prop property from the album. Hints Use bracket notation when accessing object properties with variables. Push is an array method you can read about on Mozilla Developer Network. You may refer back to Manipulating Complex Objects Introducing JavaScript Object Notation (JSON) for a refresher. Ans: // Setup var collection = { "2548": { "album": "Slippery When Wet", "artist": "Bon Jovi", "tracks": [ "Let It Rock", "You Give Love a Bad Name" ] }, "2468": { "album": "1999", "artist": "Prince", "tracks": [ "1999", "Little Red Corvette" ] }, "1245": { "artist": "Robert Palmer", "tracks": [ ] }, "5439": { "album": "ABBA Gold" } }; // Keep a copy of the collection for tests var collectionCopy = JSON.parse(JSON.stringify(collection)); // Only change code below this line function updateRecords(id, prop, value) { if(prop !== "tracks" && value !== ""){ collection[id][prop] = value; //I noticed some posts where they had an else if then an if for this one. This worked for me. Simple enough. }else if(prop == "tracks" && !collection[id].hasOwnProperty("tracks")){ collection[id][prop] = []; collection[id][prop].push(value); }else if(prop == "tracks" && value !== ""){ collection[id][prop].push(value); }else if(value === ""){ delete collection[id][prop]; } return collection; } // Alter values below to test your code updateRecords(5439, "artist", "ABBA");

Add New Properties to a JavaScript Object

You can add new properties to existing JavaScript objects the same way you would modify them. Here's how we would add a "bark" property to ourDog: ourDog.bark = "bow-wow"; or ourDog["bark"] = "bow-wow"; Now when we evaluate ourDog.bark, we'll get his bark, "bow-wow". Ans: // Setup var myDog = { "name": "Happy Coder", "legs": 4, "tails": 1, "friends": ["freeCodeCamp Campers"] }; // Only change code below this line. myDog.bark = "woof";

Nest one Array within Another Array

You can also nest arrays within other arrays, like this: [["Bulls", 23], ["White Sox", 45]]. This is also called a Multi-dimensional Array. var myArray = [["Tom", 24], ["Ben", 22]];

Use Bracket Notation to Find the Nth Character in a String

You can also use bracket notation to get the character at other positions within a string. Remember that computers start counting at 0, so the first character is actually the zeroth character. Let's try to set thirdLetterOfLastName to equal the third letter of the lastName variable using bracket notation. Hint Try looking at the secondLetterOfFirstName variable declaration if you get stuck. var lastName = "Lovelace"; var thirdLetterOfLastName = lastName[2];

Decrement a Number with JavaScript

You can easily decrement or decrease a variable by one with the -- operator. i--; is the equivalent of i = i - 1; Note The entire line becomes i--;, eliminating the need for the equal sign. myVar--;

Increment a Number with JavaScript

You can easily increment or add one to a variable with the ++ operator. i++; is the equivalent of i = i + 1; Note The entire line becomes i++;, eliminating the need for the equal sign. var myVar = 87; // Only change code below this line myVar++; (myVar = myVar + 1;)

Find the Length of a String

You can find the length of a String value by writing .length after the string variable or string literal. "Alan Peter".length; // 10 For example, if we created a variable var firstName = "Charles", we could find out how long the string "Charles" is by using the firstName.length property. // Setup var lastNameLength = 0; var lastName = "Lovelace"; // Only change code below this line. lastNameLength = lastName.length;

Iterate with JavaScript While Loops

You can run the same code multiple times by using a loop. The first type of loop we will learn is called a "while" loop because it runs "while" a specified condition is true and stops once that condition is no longer true. var ourArray = []; var i = 0; while(i < 5) { ourArray.push(i); i++; } Let's try getting a while loop to work by pushing values to an array. Ans: // Setup var myArray = []; // Only change code below this line. var i = 0; while(i < 5) { myArray.push(i); i++; }

Iterate with JavaScript For Loops

You can run the same code multiple times by using a loop. The most common type of JavaScript loop is called a "for loop" because it runs "for" a specific number of times. For loops are declared with three optional expressions separated by semicolons: for ([initialization]; [condition]; [final-expression]) The initialization statement is executed one time only before the loop starts. It is typically used to define and setup your loop variable. The 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 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. In the following example we initialize with i = 0 and iterate while our condition i < 5 is true. We'll increment i by 1 in each loop iteration with i++ as our final-expression. var ourArray = []; for (var i = 0; i < 5; i++) { ourArray.push(i); } ourArray will now contain [0,1,2,3,4]. Ans: // Example var ourArray = []; for (var i = 0; i < 5; i++) { ourArray.push(i); } // Setup var myArray = []; // Only change code below this line. for (var i = 1; i < 6; i++) { myArray.push(i); }

Iterate with JavaScript Do...While Loops

You can run the same code multiple times by using a loop. The next type of loop you will learn is called a "do...while" loop because it first will "do" one pass of the code inside the loop no matter what, and then it runs "while" a specified condition is true and stops once that condition is no longer true. Let's look at an example. var ourArray = []; var i = 0; do { ourArray.push(i); i++; } while (i < 5); This behaves just as you would expect with any other type of loop, and the resulting array will look like [0, 1, 2, 3, 4]. However, what makes the do...while different from other loops is how it behaves when the condition fails on the first check. Let's see this in action. Here is a regular while loop that will run the code in the loop as long as i < 5. var ourArray = []; var i = 5; while (i < 5) { ourArray.push(i); i++; } Notice that we initialize the value of i to be 5. When we execute the next line, we notice that i is not less than 5. So we do not execute the code inside the loop. The result is that ourArray will end up with nothing added to it, so it will still look like this [] when all the code in the example above finishes running. Now, take a look at a do...while loop. var ourArray = []; var i = 5; do { ourArray.push(i); i++; } while (i < 5); In this case, we initialize the value of i as 5, just like we did with the while loop. When we get to the next line, there is no check for the value of i, so we go to the code inside the curly braces and execute it. We will add one element to the array and increment i before we get to the condition check. Then, when we get to checking if i < 5 see that i is now 6, which fails the conditional check. So we exit the loop and are done. At the end of the above example, the value of ourArray is [5]. Essentially, a do...while loop ensures that the code inside the loop will run at least once. Let's try getting a do...while loop to work by pushing values to an array. Ans: var myArray = []; var i = 10; do { myArray.push(i) i++ } while (i < 5)

Build JavaScript Objects

You may have heard the term object before. 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 properties. Objects are useful for storing data in a structured way, and can represent real world objects, like a cat. Here's a sample cat object: var cat = { "name": "Whiskers", "legs": 4, "tails": 1, "enemies": ["Water", "Dogs"] }; In this example, all the properties are stored as strings, such as - "name", "legs", and "tails". However, you can also use numbers as properties. You can even omit the quotes for single-word string properties, as follows: var anotherObject = { make: "Ford", 5: "five", "model": "focus" }; However, if your object has any non-string properties, JavaScript will automatically typecast them as strings. Make an object that represents a dog called myDog which contains the properties "name" (a string), "legs", "tails" and "friends". You can set these object properties to whatever values you want, as long "name" is a string, "legs" and "tails" are numbers, and "friends" is an array. Ans: var myDog = { "name": "Rocco", "legs": 4, "tails": 1, "friends":["everyone"] };

Returning Boolean Values from Functions

You may recall from Comparison with the Equality Operator that all comparison operators return a boolean true or false value. Sometimes people use an if/else statement to do a comparison, like this: function isEqual(a,b) { if (a === b) { return true; } else { return false; } } But there's a better way to do this. Since === returns true or false, we can return the result of the comparison: function isEqual(a,b) { return a === b; } Ans: function isLess(a, b) { // Fix this code return a < b; } // Change these values to test isLess(10, 15);

Chaining If Else Statements

if/else statements can be chained together for complex logic. Here is pseudocode of multiple chained if / else if statements: if (condition1) { statement1 } else if (condition2) { statement2 } else if (condition3) { statement3 . . . } else { statementN } Write chained if/else if statements to fulfill the following conditions: num < 5 - return "Tiny" num < 10 - return "Small" num < 15 - return "Medium" num < 20 - return "Large" num >= 20 - return "Huge" Ans: function testSize(num) { // Only change code below this line if (num >= 20) { return "Huge"; } else if (num < 20 && num > 14) { return "Large"; } else if (num < 15 && num > 9) { return "Medium"; } else if (num < 10 && num > 4) { return "Small"; } else { return "Tiny"; } return "Change Me"; // Only change code above this line } // Change this value to test testSize(7);

Manipulate Arrays With shift()

pop() always removes the last element of an array. What if you want to remove the first? That's where .shift() comes in. It works just like .pop(), except it removes the first element instead of the last. Ans: // Setup var myArray = [["John", 23], ["dog", 3]]; // Only change code below this line. var removedFromMyArray = myArray.shift();

Compound Assignment With Augmented Subtraction

var a = 11; var b = 9; var c = 3; // Only modify code below this line a -= 6; b -=15; c -= 1;

Compound Assignment With Augmented Division

var a = 48; var b = 108; var c = 33; // Only modify code below this line a /= 12; b /= 4; c /= 11;

Compound Assignment With Augmented Multiplication

var a = 5; var b = 12; var c = 4.6; // Only modify code below this line a *= 5; b *= 3; c *= 10;

Subtract One Number from Another with JavaScript

var difference = 45 - 33;

Multiply Two Decimals with JavaScript

var product = 2.0 * 2.5;

Multiply Two Numbers with JavaScript

var product = 8 * 10;

Divide One Decimal by Another with JavaScript

var quotient = 4.4 / 2.0; // Fix this line

Divide One Number by Another with JavaScript

var quotient = 66 / 33;

Appending Variables to Strings

var someAdjective = "exciting!" var myStr = "Learning to code is "; myStr += someAdjective;


Set pelajaran terkait

Pediatric Nursing - Cardiac Disorders

View Set

Ethics Theme 3 - Teleological Ethics

View Set

Monopolistic Competition and Oligopoly Master

View Set

Mother Baby Study Questions Week 2

View Set

3.2.5. Metabolic syndrome, arthritis and inflammation

View Set

Chapter 4: Variable Costing and Segment Reporting: Tools For Management

View Set

Executive Branch Multiple Choice

View Set

Endocrine System - Hormones & Glands

View Set