Skip to main content

React - Lifting State

Jan 25, 2023CHS
Loading...

More JavaScript Posts

power set

Nov 19, 2022CodeCatch

0 likes • 2 views

const powerset = arr =>
arr.reduce((a, v) => a.concat(a.map(r => r.concat(v))), [[]]);
powerset([1, 2]); // [[], [1], [2], [1, 2]]

getSelectedText

Nov 18, 2022AustinLeath

0 likes • 0 views

// Get the text that the user has selected
const getSelectedText = () => window.getSelection().toString();
getSelectedText();

Flatten an array

Nov 19, 2022CodeCatch

0 likes • 1 view

const flat = arr => [].concat.apply([], arr.map(a => Array.isArray(a) ? flat(a) : a));
// Or
const flat = arr => arr.reduce((a, b) => Array.isArray(b) ? [...a, ...flat(b)] : [...a, b], []);
// Or
// See the browser compatibility at https://caniuse.com/#feat=array-flat
const flat = arr => arr.flat();
// Example
flat(['cat', ['lion', 'tiger']]); // ['cat', 'lion', 'tiger']

Temperature Converter

Nov 19, 2022CodeCatch

0 likes • 1 view

const celsiusToFahrenheit = (celsius) => celsius * 9/5 + 32;
const fahrenheitToCelsius = (fahrenheit) => (fahrenheit - 32) * 5/9;
// Examples
celsiusToFahrenheit(15); // 59
celsiusToFahrenheit(0); // 32
celsiusToFahrenheit(-20); // -4
fahrenheitToCelsius(59); // 15
fahrenheitToCelsius(32); // 0

UNT canvas file finder utility

Nov 18, 2022AustinLeath

0 likes • 0 views

//use this with canvas file finder
function NewTab(testing) {
window.open(testing, "_blank");
}
for(test in results) {
var testing = 'https://unt.instructure.com/files/' + test + '/download';
NewTab(testing);
}

LeetCode Contains Duplicate

Sep 13, 2023CHS

0 likes • 2 views

/**
* @param {number[]} nums
* @return {boolean}
*/
var containsDuplicate = function(nums) {
return new Set(nums).size !== nums.length
};