advent of code help

I am trying to see if anyone can help me redo my code I am struggling with my code. It seems like it worked but then it does not
demoData = `two1nine
eightwothree
abcone2threexyz
xtwone3four
4nineeightseven2
zoneight234
7pqrstsixteen`
let demoStrings = demoData.split(/\n/);

let wordNumObj = {
one: 1,
two: 2,
three: 3,
four: 4,
five: 5,
six: 6,
seven: 7,
eight: 8,
nine: 9,
};

let numStr = "";
let numArr = [];
let splitStr = "";
let currentString = "";
let newStr ="";
function getNumbersFromStrings() {
for (let i = 0; i < demoStrings.length; i++) {
currentString = demoStrings[i].trim();
for (let x = 0; x < currentString.length; x++) {
splitStr += currentString.charAt(x);
for (key in wordNumObj) {
if (splitStr.match(key)) {
numStr += wordNumObj[key];
newStr = splitStr.slice(-1);
splitStr = newStr;
} else if (splitStr.match(wordNumObj[key])) {
numStr += wordNumObj[key];
splitStr = "";
}
// console.log(splitStr, 'and the iteration',i)
} // end for key in wordNumbObj
} // end loop for currentString.Length

numStr = numStr.charAt(0) + numStr.charAt(numStr.length - 1);
numArr.push(Number(numStr.trim()));
numStr = "";

} // end for loop for demoStrings

const total = numArr.reduce((accumulator, currentValue) => {
return accumulator + currentValue;
}, 0);
console.log(total, "is the total");
}
getNumbersFromStrings();

// the answer I got 53587 it is to low
demoData = `two1nine
eightwothree
abcone2threexyz
xtwone3four
4nineeightseven2
zoneight234
7pqrstsixteen`
let demoStrings = demoData.split(/\n/);

let wordNumObj = {
one: 1,
two: 2,
three: 3,
four: 4,
five: 5,
six: 6,
seven: 7,
eight: 8,
nine: 9,
};

let numStr = "";
let numArr = [];
let splitStr = "";
let currentString = "";
let newStr ="";
function getNumbersFromStrings() {
for (let i = 0; i < demoStrings.length; i++) {
currentString = demoStrings[i].trim();
for (let x = 0; x < currentString.length; x++) {
splitStr += currentString.charAt(x);
for (key in wordNumObj) {
if (splitStr.match(key)) {
numStr += wordNumObj[key];
newStr = splitStr.slice(-1);
splitStr = newStr;
} else if (splitStr.match(wordNumObj[key])) {
numStr += wordNumObj[key];
splitStr = "";
}
// console.log(splitStr, 'and the iteration',i)
} // end for key in wordNumbObj
} // end loop for currentString.Length

numStr = numStr.charAt(0) + numStr.charAt(numStr.length - 1);
numArr.push(Number(numStr.trim()));
numStr = "";

} // end for loop for demoStrings

const total = numArr.reduce((accumulator, currentValue) => {
return accumulator + currentValue;
}, 0);
console.log(total, "is the total");
}
getNumbersFromStrings();

// the answer I got 53587 it is to low
2 Replies
MarkBoots
MarkBoots12mo ago
checl out #advent-challenges-2023
althepal78
althepal78OP12mo ago
it worked out it was that I didn't copy the information properly even though I did a ctrl a ctrl v it did not work properly for some reason lol
Want results from more Discord servers?
Add your server