Loading...
More JavaScript Posts
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));
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
// `arr` is an arrayconst clone = arr => arr.slice(0);// Orconst clone = arr => [...arr];// Orconst clone = arr => Array.from(arr);// Orconst clone = arr => arr.map(x => x);// Orconst clone = arr => JSON.parse(JSON.stringify(arr));// Orconst clone = arr => arr.concat([]);
// `date` is a `Date` objectconst formatYmd = date => date.toISOString().slice(0, 10);// ExampleformatYmd(new Date()); // 2020-05-06
//JavaScript program to swap two variables//take input from the userslet a = prompt('Enter the first variable: ');let b = prompt('Enter the second variable: ');// XOR operatora = a ^ bb = a ^ ba = a ^ bconsole.log(`The value of a after swapping: ${a}`);console.log(`The value of b after swapping: ${b}`);
/*** @param {number[]} nums* @return {boolean}*/var containsDuplicate = function(nums) {return new Set(nums).size !== nums.length};