Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save alexcrist/4ea487d5d70220fb8e76d3fc69a240fc to your computer and use it in GitHub Desktop.
Save alexcrist/4ea487d5d70220fb8e76d3fc69a240fc to your computer and use it in GitHub Desktop.
// Break =================================
// Break ends a loop immediately
console.log('=== BREAK ===');
for (let i = 0; i < 10; i++) {
console.log(i);
if (i === 5) {
break;
}
}
console.log('done');
// Continue =================================
// Skips an iteration of the loop
console.log('=== CONTINUE ===');
for (let i = 0; i < 10; i++) {
if (i === 3) {
continue;
}
console.log(i);
}
// Switch, case statement =================================
const name = 'Lee';
if (name === 'Lee') {
// DO something
} else if (name === 'Mesi') {
// Do something else
} else if (name === 'Andy') {
// Etc
} else {
// If none of the conditions are true
}
// Switch case statement is identical to a bunch of if else statements
switch (name) {
case 'Lee':
// Do something
break;
case 'Mesi':
// Do something
break;
case 'Andy':
// Etc
break;
default:
// If none of the conditions are true
break;
}
// .split, .join =================================
// Input: 'a bunch of words'
// 1. str.split(' ')
// => ['a', 'bunch', 'of', 'words']
// 2. TODO: loop through each, capitalize first letter
// => ['A', 'Bunch', 'Of', 'Words']
// 3. arr.join(' ');
// Output: 'A Bunch Of Words'
// Commaify, replaces spaces with commas
// Input: 'a bunch of words'
// Output: 'a,bunch,of,words'
function commaify(str) {
const arrOfWords = str.split(' ');
return arrOfWords.join(',');
}
console.log(commaify('a bunch of words'));
// How does split work?
// str.split(' ')
// 'a bunch of words'
// => ['a', 'bunch', 'of', 'words']
// How does join work?
// arr.join('')
// ['a', 'bunch', 'of', 'words']
// => 'a bunch of words'
// .reduce, .map, .filter =================================
// Reduce =================================
// Take in an array of values, create a new value based on those values
// Sum an arary of numbers
// Input: [1, 2, 3]
// Ouput: 6
function sum(arrayOfNumbers) {
let output = 0;
for (const num of arrayOfNumbers) {
output += num;
}
return output;
}
const arrowFunction = () => {
};
function functionDelcaration() {
}
function sum2(arrayOfNumbers) {
const sum = arrayOfNumbers.reduce(
(sum, num) => sum + num,
0
);
return sum;
}
// Map =================================
// Takes in array, maps each value to another value (output is array)
// Doubles each number in an array
// Input: [1, 2, 3]
// Output: [2, 4, 6]
function arrayDoubler(numArray) {
let output = [];
for (const num of numArray) {
output.push(num * 2);
}
return output;
}
function arrayDoubler2(numArray) {
const output = numArray.map(
(num) => num * 2
);
return output;
}
// Filter =================================
// Take in an array, return an array with some values filtered out
// Filters out all values greater than 5
// Input: [3, 6, 10]
// Ouput: [3]
function lessThan5(arr) {
let output = [];
for (const num of arr) {
if (num < 5) {
output.push(num);
}
}
return output;
}
function lessThan5Two(arr) {
const output = arr.filter(
(num) => num < 5
);
return output;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment