Loading...
More JavaScript Posts
const primes = num => {let arr = Array.from({ length: num - 1 }).map((x, i) => i + 2),sqroot = Math.floor(Math.sqrt(num)),numsTillSqroot = Array.from({ length: sqroot - 1 }).map((x, i) => i + 2);numsTillSqroot.forEach(x => (arr = arr.filter(y => y % x !== 0 || y === x)));return arr;};primes(10); // [2, 3, 5, 7]
// promisify(f, true) to get array of resultsfunction promisify(f, manyArgs = false) {return function (...args) {return new Promise((resolve, reject) => {function callback(err, ...results) { // our custom callback for fif (err) {reject(err);} else {// resolve with all callback results if manyArgs is specifiedresolve(manyArgs ? results : results[0]);}}args.push(callback);f.call(this, ...args);});};}// usage:f = promisify(f, true);f(...).then(arrayOfResults => ..., err => ...);
import { createSlice } from "@reduxjs/toolkit";const alert = createSlice({name: "alert",initialState: {msg: "",status: "",},reducers: {set_alert: (state, action) => {return {...state,msg: action.payload.msg,status: action.payload.status,};},clear_alert: (state, action) => {return {...state,msg: "",status: "",};},},});export default alert.reducer;const { set_alert, clear_alert } = alert.actions;export const setAlert = (msg, status) => dispatch => {dispatch(set_alert({ msg, status }));setTimeout(() => dispatch(clear_alert()), 4000);};export const clearAlert = () => dispatch => {dispatch(clear_alert());};
function Welcome(props) {return <h1>Hello, {props.name}</h1>;}function App() {return (<div><Welcome name="Sara" /><Welcome name="Cahal" /><Welcome name="Edite" /></div>);}ReactDOM.render(<App />,document.getElementById('root'));
const getURLParameters = url =>(url.match(/([^?=&]+)(=([^&]*))/g) || []).reduce((a, v) => ((a[v.slice(0, v.indexOf('='))] = v.slice(v.indexOf('=') + 1)), a),{});
const insertionSort = arr =>arr.reduce((acc, x) => {if (!acc.length) return [x];acc.some((y, j) => {if (x <= y) {acc.splice(j, 0, x);return true;}if (x > y && j === acc.length - 1) {acc.splice(j + 1, 0, x);return true;}return false;});return acc;}, []);insertionSort([6, 3, 4, 1]); // [1, 3, 4, 6]