this post was submitted on 16 Nov 2023
710 points (95.1% liked)

memes

10117 readers
3561 users here now

Community rules

1. Be civilNo trolling, bigotry or other insulting / annoying behaviour

2. No politicsThis is non-politics community. For political memes please go to [email protected]

3. No recent repostsCheck for reposts when posting a meme, you can only repost after 1 month

4. No botsNo bots without the express approval of the mods or the admins

5. No Spam/AdsNo advertisements or spam. This is an instance rule and the only way to live.

Sister communities

founded 1 year ago
MODERATORS
 
you are viewing a single comment's thread
view the rest of the comments
[–] [email protected] 8 points 11 months ago (1 children)
try {
  const joke = allProgrammingJokes[Math.floor(Math.random() * allProgrammingJokes.length)];
  if (!getJoke(joke)) {
    throw new Error("Joke not understood");
  }
} catch (error) {
  console.log("lol *upvotes*");
}

function getJoke(joke) {
  // This function is intentionally flawed to always return false.
  // It's a part of the joke!
  return false;
}
[–] [email protected] 8 points 11 months ago (2 children)

allProgrammingJokes[Math.floor(Math.random() * allProgrammingJokes.length)]

This might throw array index out of bounds errors.

[–] [email protected] 9 points 11 months ago

ECMAScript spec says Math.random must be less than 1. I was about to stop there, but a thought occurred to me: could the multiply with a float make a number large enough to floor to a different value for large enough values? 🤔

I imagine it'd have to be a ridiculously large number to amount enough floating point imprecision to matter, if so.

[–] [email protected] 2 points 11 months ago