Skip to main content

Ticking clock

Nov 19, 2022CodeCatch
Loading...

More JavaScript Posts

Redux Setup

Mar 11, 2021CHS

0 likes • 0 views

import { configureStore } from "@reduxjs/toolkit";
import { combineReducers } from "redux";
import profile from "./profile";
import auth from "./auth";
import alert from "./alert";
const reducer = combineReducers({
profile,
auth,
alert,
});
const store = configureStore({ reducer });
export default store;

URL Search Term Generator

Jan 17, 2021CHS

0 likes • 0 views

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;
};

find primes

Nov 19, 2022CodeCatch

0 likes • 0 views

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]

Bitwise XOR operator

Nov 19, 2022CodeCatch

0 likes • 1 view

//JavaScript program to swap two variables
//take input from the users
let a = prompt('Enter the first variable: ');
let b = prompt('Enter the second variable: ');
// XOR operator
a = a ^ b
b = a ^ b
a = a ^ b
console.log(`The value of a after swapping: ${a}`);
console.log(`The value of b after swapping: ${b}`);

containsDuplicate

Nov 19, 2022CodeCatch

0 likes • 0 views

let nums = [1,2,3,1]
var containsDuplicate = function(nums) {
let obj = {};
for(let i =0; i< nums.length; i++){
if(obj[nums[i]]){
obj[nums[i]] += 1;
return true;
}else{
obj[nums[i]] = 1;
}
}
return false;
};
console.log(containsDuplicate(nums));

Clone an array

Nov 19, 2022CodeCatch

0 likes • 1 view

// `arr` is an array
const clone = arr => arr.slice(0);
// Or
const clone = arr => [...arr];
// Or
const clone = arr => Array.from(arr);
// Or
const clone = arr => arr.map(x => x);
// Or
const clone = arr => JSON.parse(JSON.stringify(arr));
// Or
const clone = arr => arr.concat([]);