Loading...
More JavaScript Posts
// There are n rings and each ring is either red, green, or blue. The rings are distributed across ten rods labeled from 0 to 9.// You are given a string rings of length 2n that describes the n rings that are placed onto the rods. Every two characters in rings forms a color-position pair that is used to describe each ring where:// The first character of the ith pair denotes the ith ring's color ('R', 'G', 'B').// The second character of the ith pair denotes the rod that the ith ring is placed on ('0' to '9').// For example, "R3G2B1" describes n == 3 rings: a red ring placed onto the rod labeled 3, a green ring placed onto the rod labeled 2, and a blue ring placed onto the rod labeled 1.// Return the number of rods that have all three colors of rings on them.let rings = "B0B6G0R6R0R6G9";var countPoints = function(rings) {let sum = 0;// Always 10 Rodsfor (let i = 0; i < 10; i++) {if (rings.includes(`B${i}`) && rings.includes(`G${i}`) && rings.includes(`R${i}`)) {sum+=1;}}return sum;};console.log(countPoints(rings));
const getSearchTerm = delimiter => {let searchTerm = "";for (let i = 1; i < commands.length - 1; i++)searchTerm = searchTerm + commands[i] + delimiter;searchTerm += commands[commands.length - 1];return searchTerm;};
export const Main = () => {const [title, setTitle] = useState('');return (<ChildComponenttitle={title}onChange={e => setTitle(e.target.value)}/>);};export const ChildComponent = ({ title, onChange }) => {return (<Box component="form" onSubmit={() => {}} noValidate mt={5}><TextFieldfullWidthid="title"label="Title"name="title"autoComplete="title"autoFocuscolor="primary"variant="filled"InputLabelProps={{ shrink: true }}value={title}onChange={onChange}/></Box>);};
const unzipWith = (arr, fn) =>arr.reduce((acc, val) => (val.forEach((v, i) => acc[i].push(v)), acc),Array.from({length: Math.max(...arr.map(x => x.length))}).map(x => [])).map(val => fn(...val));unzipWith([[1, 10, 100],[2, 20, 200],],(...args) => args.reduce((acc, v) => acc + v, 0));// [3, 30, 300]
const levenshteinDistance = (s, t) => {if (!s.length) return t.length;if (!t.length) return s.length;const arr = [];for (let i = 0; i <= t.length; i++) {arr[i] = [i];for (let j = 1; j <= s.length; j++) {arr[i][j] =i === 0? j: Math.min(arr[i - 1][j] + 1,arr[i][j - 1] + 1,arr[i - 1][j - 1] + (s[j - 1] === t[i - 1] ? 0 : 1));}}return arr[t.length][s.length];};levenshteinDistance('duck', 'dark'); // 2
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]