Skip to content

Instantly share code, notes, and snippets.

@LGitHub-sprout
LGitHub-sprout / arrayMethods.js
Last active June 3, 2023 17:29
splice, push, map, slice, concat, join, split, indexOf, filter, reduce, sort, thisArg, spread, Lookup Table, new Set
// 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 }
@LGitHub-sprout
LGitHub-sprout / exercises.js
Last active June 29, 2023 15:28
TOP Foundations Exercises #8-12
// 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 === '+') {
@LGitHub-sprout
LGitHub-sprout / someExamples.js
Last active December 13, 2022 15:53
Examples from some() method MDN page - Nested Returns.
// 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'.
@LGitHub-sprout
LGitHub-sprout / computedProperties.js
Last active December 2, 2022 00:09
Objects and Computed Properties
/*
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
*/
@LGitHub-sprout
LGitHub-sprout / reduceExamples.js
Last active June 5, 2023 20:13
filter, concat, recursion, tally, keyed object, lookup table, piping/pipeline, destructuring
// 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: [
@LGitHub-sprout
LGitHub-sprout / thisKeyword.js
Last active November 15, 2022 11:11
Info on keyword this
// 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,
};
@LGitHub-sprout
LGitHub-sprout / stringManipulation.js
Created November 11, 2022 01:08
String to array;
/* 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;
@LGitHub-sprout
LGitHub-sprout / higherOrderFunctions.js
Last active March 4, 2023 18:02
@toby's Higher Order Functions
// @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?
@LGitHub-sprout
LGitHub-sprout / sortAlgorithms.js
Last active May 30, 2023 18:44
Types of sorting algorithms; decrement vs increment; recursion.
// 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]
@LGitHub-sprout
LGitHub-sprout / objectsIntro.js
Last active June 8, 2023 15:55
Wes Bos Array Cardio I & II: sort, isEmpty check, Constructors (calculator), destructuring,
// 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 }
];