Published
Edited
Dec 5, 2019
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
isValid = function(pwd) {
// is a six digit number
if (pwd >= 1000000 || pwd <= 99999) {
return false;
}
// within input range
if (pwd < a || pwd > b) {
return false;
}
// has two adjacent digits the same
if (!/(?:00|11|22|33|44|55|66|77|88|99)/.test(`${pwd}`)) {
return false;
}
// from left to right digits never decrease
return [...`${pwd}`].every((digit, index, arr) => {
return index === 0 || parseInt(digit, 10) >= parseInt(arr[index - 1], 10);
});
}
Insert cell
{
const passwords = new Set();
for (let i = a; i < b; i++) {
if (isValid(i)) {
passwords.add(i);
}
}
return passwords.size;
}
Insert cell
Insert cell
charCounts = function(i) {
const counts = {};
[...`${i}`].forEach((char) => {
counts[char] = counts[char] ? counts[char] + 1 : 1;
});
return counts;
}
Insert cell
{
const passwords = new Set();
for (let i = a; i < b; i++) {
if (isValid(i) && Object.values(charCounts(i)).includes(2)) {
passwords.add(i);
}
}
return passwords;
}
Insert cell

Purpose-built for displays of data

Observable is your go-to platform for exploring data and creating expressive data visualizations. Use reactive JavaScript notebooks for prototyping and a collaborative canvas for visual data exploration and dashboard creation.
Learn more