Skip to main content

stripe api json iteration

Nov 18, 2022AustinLeath
Loading...

More JavaScript Posts

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

QM Helper

Mar 22, 2022LeifMessinger

0 likes • 1 view

//QM Helper by Leif Messinger
//Groups numbers by number of bits and shows their binary representations.
//To be used on x.com
const minterms = prompt("Enter your minterms separated by commas").split(",").map(x => parseInt(x.trim()));
const maxNumBits = minterms.reduce(function(a, b) {
return Math.max(a, Math.log2(b));
});
const bitGroups = [];
for(let i = 0; i < maxNumBits; ++i){
bitGroups.push([]);
}
for(const minterm of minterms){
let outputString = (minterm+" ");
//Count the bits
let count = 0;
for (var i = maxNumBits; i >= 0; --i) {
if((minterm >> i) & 1){
++count;
outputString += "1";
}else{
outputString += "0";
}
}
bitGroups[count].push(outputString);
}
document.body.textContent = "";
document.body.style.setProperty("white-space", "pre");
for(const group of bitGroups){
for(const outputString of group){
document.body.textContent += outputString + "\r\n";
}
}

hamming distance

Nov 19, 2022CodeCatch

0 likes • 0 views

const hammingDistance = (num1, num2) =>
((num1 ^ num2).toString(2).match(/1/g) || '').length;
hammingDistance(2, 3); // 1

Untitled

Apr 14, 2023Helper

0 likes • 8 views

const quickSort = arr => {
const a = [...arr];
if (a.length < 2) return a;
const pivotIndex = Math.floor(arr.length / 2);
const pivot = a[pivotIndex];
const [lo, hi] = a.reduce(
(acc, val, i) => {
if (val < pivot || (val === pivot && i != pivotIndex)) {
acc[0].push(val);
} else if (val > pivot) {
acc[1].push(val);
}
return acc;
},
[[], []]
);
return [...quickSort(lo), pivot, ...quickSort(hi)];
};
console.log(quickSort([1, 6, 1, 5, 3, 2, 1, 4])) // [1, 1, 1, 2, 3, 4, 5, 6]

Destructuring Assignment

Nov 19, 2022CodeCatch

0 likes • 0 views

//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: ');
//using destructuring assignment
[a, b] = [b, a];
console.log(`The value of a after swapping: ${a}`);
console.log(`The value of b after swapping: ${b}`);

linear search

Nov 19, 2022CodeCatch

0 likes • 0 views

const linearSearch = (arr, item) => {
for (const i in arr) {
if (arr[i] === item) return +i;
}
return -1;
};
linearSearch([2, 9, 9], 9); // 1
linearSearch([2, 9, 9], 7); // -1