Variables that have not been initialized store the primitive data type undefined.
var a;
console.log(a);
// Prints: undefined
Math.random();
console.log(Math.random());
// Prints: 0 - 0.9
Math.random();
// Returns a number between 0 and 1
Math.random();
// ☝️ Math is the library
String .length
let message = 'good nite';
console.log(message.length);
// Prints: 9
Math.floor()
console.log(Math.floor(5.95));
// Prints: 5
Remainder / Modulo Operator
// calculates # of weeks in a year, rounds down to nearest integer
const weeksInYear = Math.floor(365/7);
// calcuates the number of days left over after 365 is divded by 7
const daysLeftOver = 365 % 7 ;
console.log("A year has " + weeksInYear + " weeks and " + daysLeftOver + " days");
Assignment Operators
An assignment operator assigns a value to its left operand based on the value of its right operand. Here are some of them:
let number = 100;
// Both statements will add 10
number = number + 10;
number += 10;
console.log(number);
// Prints: 120
Template Literals
let name = "Vicma";
console.log(`Hello, ${name}`);
// Prints: Hello, Vicma
console.log(`Burrito is ${6+8} years old.`);
// Prints: Burrito is 14 years old.
Let Keyword
let creates a local variable in JavaScript & can be re-assigned. Initialization during the declaration of a let variable is optional. A let variable will contain undefined if nothing is assigned to it.
let count;
console.log(count); // Prints: undefined
count = 10;
console.log(count); // Prints: 10
true || false; // true
10 > 5 || 10 > 20; // true
false || false; // false
10 > 100 || 10 > 20; // false
A | B | A B |
---|---|---|
false | false | false |
false | true | true |
true | false | true |
true | true | true |
let excited = true;
console.log(!excited); // Prints false
let sleepy = false;
console.log(!sleepy); // Prints true
js
Essentially, the ! operator will either take a true value and pass back false, or it will take a false value and pass back true.
if (stopLight === 'green' && pedestrians === 0) {
console.log('Go!');
} else {
console.log('Stop');
}
js
When using the && operator, both conditions must evaluate to true for the entire condition to evaluate to true and execute. Otherwise, if either condition is false, the && condition will evaluate to false and the else block will execute.
If we only care about either condition being true, we can use the || operator:
if (day === 'Saturday' || day === 'Sunday') {
console.log('Enjoy the weekend!');
} else {
console.log('Do some work.');
}
js
When using the || operator, only one of the conditions must
evaluate to true for the overall statement to evaluate to true.
In the code example above, if either day =
‘Saturday’ or day =
‘Sunday’ evaluates to true the *if*‘s condition will evaluate to
true and its code block will execute. If the first condition in an
|| statement evaluates to true, the second condition won’t
even be checked. Only if day =
‘Saturday’ evaluates to false
will day =
‘Sunday’ be evaluated. The code in the else
statement above will execute only if both comparisons evaluate to
false.
let twentyThree = 'XXIII';
let romanNumerals = ['XXI', 'XXII'];
romanNumerals.unshift('XIX', 'XX');
// *romanNumerals* would have the value *['XIX', 'XX', 'XXI', 'XXII']*.
romanNumerals.push(twentyThree);
// *romanNumerals* would have the value *['XIX', 'XX', 'XXI', 'XXII', 'XXIII']*.
//Notice that we can also pass variables, which allows us even greater flexibility in dynamically modifying our array's data.
let greetings = ['whats up?', 'hello', 'see ya!'];
greetings.pop();
greetings.shift();
- greetings would have the value ['hello'].
let popped = greetings.pop();
function popShift(arr) {
let popped = arr.pop();
let shifted = arr.shift() ;
let answer = popped +' '+ shifted; //I'm not sure if this declariton it's correct
return [answer];
}
console.log(popShift(['challenge', 'is', 'not', 'complete']));
Please leave a comment or open a issue with a refactor of the above code
function popShift(arr) {
let popped = arr.pop();
let shifted = arr.shift() ;
return [shifted, popped];
}
console.log(popShift(['challenge', 'is', 'not', 'complete']));
const numbers = [10, 11, 12, 12, 15];
const startIndex = 3;
const amountToDelete = 1;
numbers.splice(startIndex, amountToDelete, 13, 14);
console.log(numbers);
let weatherConditions = ['rain', 'snow', 'sleet', 'hail', 'clear'];
let todaysWeather = weatherConditions.slice(1, 3);
function forecast(arr) {
// Only change code below this line
return arr.slice(2,4);
}
// Only change code above this line
console.log(forecast(['cold', 'rainy', 'warm', 'sunny', 'cool', 'thunderstorms']));
In practice, we can use the spread operator to copy an array like so:
let thisArray = [true, true, undefined, false, null];
let thatArray = [...thisArray]; //Output: [true, true, undefined, false, null]
function copyMachine(arr, num) {
let newArr = [];
while (num >= 1) {
// Only change code below this line
// Only change code above this line
num--;
}
return newArr;
}
// copyMachine([true, false, true], 2) should return [[true, false, true], [true, false, true]]
console.log(copyMachine([true, false, true], 2));
Solution
newArr.push([...arr]);
function spreadOut() {
let fragment = ['to', 'code'];
let sentence = ['learning', ...fragment, 'is', 'fun']
return sentence;
}
console.log(spreadOut());
let fruits = ['apples', 'pears', 'oranges', 'peaches', 'pears'];
fruits.indexOf('dates');
fruits.indexOf('oranges');
fruits.indexOf('pears');
// Output: indexOf('dates') returns -1, indexOf('oranges') returns 2, and indexOf('pears') returns 1 (the first index at which each element exists).
We have defined a function, quickCheck, that takes an array and an element as arguments. Modify the function using indexOf() so that it returns true if the passed element exists on the array, and false if it does not.
function quickCheck(arr, elem) {
// Only change code below this line
// Only change code above this line
}
console.log(quickCheck(['squash', 'onions', 'shallots'], 'mushrooms'));
Solution
if (arr.indexOf(elem) >= 0){
return true;
} else {
return false;
}
Refactoring
return arr.indexOf(elem) >= 0 ? true : false;
A stack is a data structure that holds a list of elements. A stack works based on the LIFO principle i.e., Last In, First out, meaning that the most recently added element is the first one to remove.
A stack has two main operations that occur only at the top of the stack: push and pop. The push operation places an element at the top of stack whereas the pop operation removes an element from the top of the stack.
The name stack comes from the analogy to a set of physical items e.g., DVD disc, books, stacked on top each other.
A stack has many applications. For example, the simplest one is to reverse a word. To do it, you push a word into the stack, letter by letter, and pop the letters from the stack.
The other applications of the stack are ÔÇ£undoÔÇØ mechanism in text editors, syntax parsing, function call, and expression conversion (infix to postfix, infix to prefix, postfix to infix, and prefix to infix).
JavaScript Array type provides the push() and pop() methods that allow you to use an array as a stack.
let stack = [];
stack.push(1);
console.log(stack); // [1]
stack.push(2);
console.log(stack); // [1,2]
stack.push(3);
console.log(stack); // [1,2,3]
stack.push(4);
console.log(stack); // [1,2,3,4]
stack.push(5);
console.log(stack); // [1,2,3,4,5]
The following figure illustrates each step in the script above.
console.log(stack.pop()); // 5
console.log(stack); // [1,2,3,4];
console.log(stack.pop()); // 4
console.log(stack); // [1,2,3];
console.log(stack.pop()); // 3
console.log(stack); // [1,2];
console.log(stack.pop()); // 2
console.log(stack); // [1];
console.log(stack.pop()); // 1
console.log(stack); // []; -> empty
console.log(stack.pop()); // undefined
The figure below illustrates each step in the script.
mkdir webpack-demo
cd webpack-demo
npm init -y
npm install webpack webpack-cli --save-dev