Published
Edited
Dec 18, 2020
Insert cell
Insert cell
Insert cell
answer_1 = {
let valid = 0;
for (const [passport, fields] of input) {
if (fields.length === 8 || (fields.length === 7 && passport.cid === ""))
valid++;
}
return valid;
}
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
answer_2 = {
const input = input2;
let valid = 0,
invalid = 0;
let filtered_valid = [];
let filtered_invalid = [];
const pushInvalid = passport => {
invalid++;
filtered_invalid.push(passport);
};
const pushValid = passport => {
valid++;
filtered_valid.push(passport);
};

const correctECL = ["amb", "blu", "brn", "gry", "grn", "hzl", "oth"];
const correctHCL = /#[0-9a-fAF]{6}/;
const correctPID = /[0-9]{9}/;
for (const [passport, fields] of input) {
// All fields have to be filled, except for Country ID
let byr = +passport.byr; // four digits; at least 1920 and at most 2002.
byr =
passport.byr.length === 4 && byr >= 1920 && byr <= 2002 ? true : false;
let iyr = +passport.iyr; // four digits; at least 2010 and at most 2020.
iyr =
passport.iyr.length === 4 && iyr >= 2010 && iyr <= 2020 ? true : false;

let eyr = +passport.eyr; // four digits; at least 2020 and at most 2030.
eyr =
passport.eyr.length === 4 && eyr >= 2020 && eyr <= 2030 ? true : false;

let hgt = false;
if (passport.hgt.endsWith("cm")) {
hgt = +passport.hgt.slice(0, Math.max(0, passport.hgt.length - 2));
hgt = hgt >= 150 && hgt <= 193 ? true : false;
} else if (passport.hgt.endsWith("in")) {
hgt = +passport.hgt.slice(0, Math.max(0, passport.hgt.length - 2));
hgt = hgt >= 59 && hgt <= 76 ? true : false;
} else hgt = false;
const hcl = correctHCL.test(passport.hcl);
const ecl = correctECL.includes(passport.ecl);
const pid = passport.pid.length === 9 && correctPID.test(passport.pid);
if (!byr || !iyr || !eyr || !hgt || !hcl || !ecl || !pid || false) {
pushInvalid(passport);
} else {
pushValid(passport);
}
}
return { filtered_valid, filtered_invalid };
}
Insert cell
Insert cell

One platform to build and deploy the best data apps

Experiment and prototype by building visualizations in live JavaScript notebooks. Collaborate with your team and decide which concepts to build out.
Use Observable Framework to build data apps locally. Use data loaders to build in any language or library, including Python, SQL, and R.
Seamlessly deploy to Observable. Test before you ship, use automatic deploy-on-commit, and ensure your projects are always up-to-date.
Learn more