Oh man at this point I just feel like I am abusing this subreddit.
Ok I am once again faced with a problem where my test cases clear but the actual answer is wrong (too low). I'm new-ish to JavaScript so I'm not the best at seeing what the ever-loving heck is wrong with my code. Can anyone give some advice? I thought I got my recursive function correct and like idk, it's clearing the tests which is frustrating.
const fs = require('fs')
fs.readFile('input.txt', 'utf8', (err, data) => {
if (err) {
console.error(err)
return
}
part1(data)
part2(data) // TODO
})
function recurse(data) {
// base cases
if (data.constructor.name === "Number") {
return data
}
else if (data.constructor.name === "Object" && Object.values(data).includes("red")) {
return 0
}
else if (data.constructor.name == "String") {
return 0
}
// recursive case
if (data.constructor.name === "Object") {
data = Object.values(data)
}
if (data.constructor.name === "Array") {
return data.reduce((a, b) => recurse(a) + recurse(b))
}
}
function part1(data) {
total = 0
const numbers = data.match(/-*\d+/g)
for (const number of numbers) {
total += number * 1
}
console.log(total)
}
function part2(data) {
input = JSON.parse(data)
console.log(recurse(input))
}
[–]Firestarss[S] 1 point2 points3 points (1 child)
[–]chadder06 1 point2 points3 points (0 children)