Skip to main content

levenshtein distance

Nov 19, 2022CodeCatch
Loading...

More JavaScript Posts

Rings and Rods

Nov 19, 2022CodeCatch

0 likes • 1 view

// 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 Rods
for (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));

compact object

Nov 19, 2022CodeCatch

0 likes • 0 views

const compactObject = val => {
const data = Array.isArray(val) ? val.filter(Boolean) : val;
return Object.keys(data).reduce(
(acc, key) => {
const value = data[key];
if (Boolean(value))
acc[key] = typeof value === 'object' ? compactObject(value) : value;
return acc;
},
Array.isArray(val) ? [] : {}
);
};
const obj = {
a: null,
b: false,
c: true,
d: 0,
e: 1,
f: '',
g: 'a',
h: [null, false, '', true, 1, 'a'],
i: { j: 0, k: false, l: 'a' }
};
compactObject(obj);
// { c: true, e: 1, g: 'a', h: [ true, 1, 'a' ], i: { l: 'a' } }

Reverse a string

Nov 19, 2022CodeCatch

0 likes • 1 view

const reverse = str => str.split('').reverse().join('');
reverse('hello world');
// Result: 'dlrow olleh'

Sequential Queue

Feb 6, 2021LeifMessinger

0 likes • 2 views

class SequentialQueue{ //if you want it to go backwards, too bad
next(){
return this.i++;
}
constructor(start = 0){
this.i = start;
}
}
const que = new SequentialQueue(0);
for(let i = 0; i < 10; i++){
console.log(que.next());
}

Heroku Production Build

Mar 11, 2021CHS

1 like • 1 view

if (process.env.NODE_ENV === "production") {
app.use(express.static("client/build"));
app.get("*", (req, res) => {
res.sendFile(path.resolve(__dirname, "client", "build", "index.html"));
});
}

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