This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Exercises and noodling from JS.info Array Methods | |
// https://javascript.info/array-methods | |
// reduce() - calculate a single value based on an array | |
// map() - iterate and return data for each element | |
const comments = [ | |
{ text: 'Love this!', id: 523423 }, | |
{ text: 'Super good', id: 823423 }, | |
{ text: 'You are the best', id: 2039842 }, | |
{ text: 'Ramen is my fav food ever', id: 123523 }, | |
{ text: 'Nice Nice Nice!', id: 542328 } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// https://www.theodinproject.com/lessons/foundations-fundamentals-part-5#assignment | |
// Calculator bits and pieces | |
// 1. The calculator functions | |
const add = (a, b) => a += b; | |
const subtract = (a, b) => a -= b; | |
const multiply = (a, b) => a *= b; | |
const divide = (a, b) => a /= b; | |
function operate(operator, num1, num2) { // still needs the operators | |
if (operator === '+') { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// MDN Example - Check Inventory (nested returns) | |
// See below link for converting any value to boolean | |
// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/some | |
const fruit = ['kiwi', 'apple', 'banana', 'avocado']; | |
// Check inventory for 'pineapple' and then 'apple' | |
/* | |
Need to pass 'arr' and 'item' param. | |
If 'fruit' arg STRICTLY equals 'item' param, | |
some should return 'true'. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
Objects and Computed Property Names; Property Accessors | |
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Object_initializer#computed_property_names | |
https://www.freecodecamp.org/news/reduce-f47a7da511a9/ | |
=> Beware of using square brackets to access properties whose names are given by EXTERNAL INPUT. | |
This may make your code susceptible to object injection attacks. | |
https://github.com/nodesecurity/eslint-plugin-security/blob/main/docs/the-dangers-of-square-bracket-notation.md#the-dangers-of-square-bracket-notation | |
*/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// https://bholmes.dev/blog/another-way-to-understand-array-reduce/ | |
// Return all the music descriptions containing 90 words or more using forEach. | |
// Then do the same thing w the reduce() method. | |
const musicData = [ | |
{ | |
name: 'Radiohead', | |
albums: [ | |
{ | |
title: 'The King of Limbs', | |
songs: [ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// https://dmitripavlutin.com/gentle-explanation-of-this-in-javascript/ | |
// https://www.youtube.com/watch?v=zE9iro4r918 | |
// https://stackoverflow.com/questions/13441307/how-does-the-this-keyword-in-javascript-act-within-an-object-literal | |
var foo = { | |
bar: function () { | |
console.log('this.baz', this.baz, 'is typeof', typeof this.baz) | |
return this.baz; | |
}, | |
baz: 1, | |
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* Manipulating strings */ | |
// Convert string to array + uncommon unicode char strings | |
// https://dev.to/sanchithasr/6-ways-to-convert-a-string-to-an-array-in-javascript-1cjg | |
const exampleString = 'A car, a man, a maraca.'.toLowerCase().split(''); | |
const lastItem = exampleString.length - 1; | |
// if (exampleString[lastItem] === '.') delete exampleString[lastItem]; | |
console.log(exampleString) | |
exampleString.forEach((el) => { | |
if (el.includes(',') || el.includes(' ') || el.includes('.')) return; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// @toby's Higher Order Functions | |
// @Chris: a higher order function is a function that either takes another function as a parameter, or returns a new function | |
const multiply = (first) => // pass in 1st param | |
(second) => first * second; // return H.O. function which waits for 2nd param | |
const triple = multiply(3); // map() callback func | |
console.log('multiply', multiply(2, 2), triple(2, 100)) | |
console.log('triple function', typeof triple, [2, 4, 6, 8].map(triple)); // returns factors or multiples of 3? |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Fisher-Yates Shuffle sort fron javascript.info | |
// https://javascript.info/array-methods#shuffle-an-array | |
function shuffle(array) { | |
// loop 2x array backwards switching 2 rando indexes e.g. [2, 1, 3] | |
for (let i = array.length - 1; i > 0; i--) { | |
// set j to a rounded rando num * (index + 1) // either 1, 2, or 3 | |
let j = Math.floor(Math.random() * (i + 1)); | |
// switches array item order, e.g., [1, 2] becomes [2, 1] | |
[array[i], array[j]] = [array[j], array[i]]; // [num, num] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// https://dmitripavlutin.com/javascript-object-destructuring/ | |
// ## Array Cardio Day 2 | |
// https://github.com/wesbos/JavaScript30/blob/master/07%20-%20Array%20Cardio%20Day%202/index-START.html | |
const people = [ | |
{ name: 'Wes', year: 1988 }, | |
{ name: 'Kait', year: 1986 }, | |
{ name: 'Irv', year: 1970 }, | |
{ name: 'Lux', year: 2015 } | |
]; |
NewerOlder