I found myself with some free time and decided to create a basic API using JavaScript. What I thought would be simple turned into a frustrating mistake.
Oddly enough, my if/else statement isn't working correctly - it only executes the code within the if
block. Even after consulting a friend who is more experienced in JavaScript, we couldn't pinpoint the issue.
Here's the snippet of code:
app.get("/number", (req, res) => {
const min = req.query.min || 0
const max = req.query.max || 100
if (min > max) {
res.status(400).json({
error: "min must be less than max. Seriously?"
})
} else {
const number = Math.floor(Math.random()*Math.max(min, max)+Math.min(min, max))
res.json({
number: number,
"your ip LEAKED 2021 not clickbait!!!11!1": req.ip
})
}
})
And here's what it outputs:
Left scratching my head over this if statement malfunction, I turn to Stack Overflow for assistance. Your help is appreciated!