I am learning JavaScript, and my friend recommended open Kattis to solve tasks. (I see now that it might not be the best for JS).
Anyway, I am doing this challenge Sibice where you are checking if matches will fit a box. Read the challenge linked for the context.
I am solving the problem, but I am not passing any of the tests Kattis has for the problem. I use readline
to take input, since you need to take the input from terminal in open Kattis.
Attached is my code. Sorry if it's messy (will take tips on that too haha), do you have any ideas? Also my first question, so I apologize if I haven't submitted it perfectly!
This is my code, and when I run it, it works fine. But Kattis is not accepting it.
const readline = require("readline");
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout,
});
rl.question(
"Enter the number of matches and the width and height of the box, separated by spaces: ",
input => {
const [numMatches, width, height] = input
.split(" ")
.map(Number);
const length = Math.sqrt(width * width + height * height);
const matchLengths = [];
const matchArray = () => {
return new Promise(resolve => {
rl.question("match length?", matchLength => {
matchLengths.push(matchLength);
resolve();
});
});
};
const askQuestion = async numMatches => {
for (i = 0; i < numMatches; i++) {
await matchArray();
}
rl.close();
};
askQuestion(numMatches).then(() => {
matchLengths.forEach(element => {
console.log(element <= length ? "DA" : "NE");
});
});
},
);