Switch() does not work with Math.random() numbers

Vincent

I am trying to make a function that involves switch() and gives me answers on specific, randomly generated numbers. Somehow my function does not run the case it is supposed to run. It only gives me the default case, no matter what the number is.

This is my code:

var i;
var girl;

function response() {
    var girl = prompt("What girl do you like?");
    var r = (Math.random() * (3 - 1 + 1) + 1).toFixed(0);
    var i = r;

    switch(i) {
        case (i == 1):
            alert(girl + " likes you as a friend.");
            break;

        case (i == 2):
            alert(girl + " does not really like you.");
            break;

        case (i == 3):
            alert(girl + " has a crush on you.");
            break;

        case (i == 4):
            alert(girl + " wants you to ask her out.");
            break;

        default:
            console.log(i);
    }
}
alex

That's not how a switch works. It compares the value for each case to the switch.

Essentially now, it is comparing the value of i multiple times to boolean values (the result of, for example i == 1).

Also, your randomness doesn't become more random by adding arithmetic with static values into the value like you did. You should replace it with 4. You also should use something like Math.ceil() (since you're ignoring the 0 value, which is also probably not a good idea), not toFixed() which returns a string.

You also don't need the parenthesis around the values to compare. If you know the range of the random number, you also probably don't need a default case (since you cover every possibility already).

You can also use r directly, no need to reassign it.

Your variables are also local to your function, so probably don't need them at the top.

Here is how I would re-write this:

function response() {
    var girl = prompt("What girl do you like?");
    var r = Math.floor(Math.random() * 4);

    switch(r) {
        case 0:
            alert(girl + " likes you as a friend.");
            break;

        case 1:
            alert(girl + " does not really like you.");
            break;

        case 2:
            alert(girl + " has a crush on you.");
            break;

        case 3:
            alert(girl + " wants you to ask her out.");
            break;

    }
}

...or even...

function response() {
    var answerSuffixes = [
        " likes you as a friend.",
        " does not really like you.",
        " has a crush on you.",
        " wants you to ask her out."
    ];
    var girl = prompt("What girl do you like?");
    var r = Math.floor(Math.random() * answerSuffixes.length);

    alert(girl + answerSuffixes[r]);
}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How does this hash function work? Are these numbers random?

From Dev

Random numbers in java does not work properly

From Dev

Im making a program that makes random math, but it does not work

From Dev

How does Math.random() EXACTLY work in Java?

From Dev

How does Math.random() generate random numbers beyond it's "native" range?

From Dev

PHP math with random numbers with random mathematical symbols

From Dev

How does two factor authentication work with random numbers?

From Dev

Math random numbers between 50 and 80

From Dev

Floating point math in c getting random numbers

From Dev

How does the distribution of Math.random()*50 + Math.random()*20 compare to Math.random()*70?

From Dev

Generate a random math Equation using Random numbers and operators in Javascript

From Dev

Why use Math.random(), and what it does

From Dev

Switch Statement, it does not work with prompt

From Dev

overridePendingTransition does not work in switch case

From Dev

Toggle switch does not work in PartialView

From Dev

How to get unique numbers from Math.random in javascript?

From Dev

How to get unique numbers from Math.random in javascript?

From Dev

how to find the appropriate math function for series with random numbers in Excel

From Dev

Creating an object of Random class or using Math.random() in order to generate random numbers

From Java

How does the Math.max.apply() work?

From Dev

How does math.min actually work?

From Dev

How does ~~ work as math.floor?

From Dev

math.pow does not work in ApplicationCraft?

From Dev

How does Java.math.BigInteger.and() work?

From Dev

How does math.min actually work?

From Dev

PHP math does not work in sql value ZERO

From Dev

Javascript - Math.floor does not work

From Dev

Why isn't Math.floor((Math.random() * 999999) + 100000) reliable for producing 6 digit numbers?

From Dev

Code does not work for large numbers

Related Related

  1. 1

    How does this hash function work? Are these numbers random?

  2. 2

    Random numbers in java does not work properly

  3. 3

    Im making a program that makes random math, but it does not work

  4. 4

    How does Math.random() EXACTLY work in Java?

  5. 5

    How does Math.random() generate random numbers beyond it's "native" range?

  6. 6

    PHP math with random numbers with random mathematical symbols

  7. 7

    How does two factor authentication work with random numbers?

  8. 8

    Math random numbers between 50 and 80

  9. 9

    Floating point math in c getting random numbers

  10. 10

    How does the distribution of Math.random()*50 + Math.random()*20 compare to Math.random()*70?

  11. 11

    Generate a random math Equation using Random numbers and operators in Javascript

  12. 12

    Why use Math.random(), and what it does

  13. 13

    Switch Statement, it does not work with prompt

  14. 14

    overridePendingTransition does not work in switch case

  15. 15

    Toggle switch does not work in PartialView

  16. 16

    How to get unique numbers from Math.random in javascript?

  17. 17

    How to get unique numbers from Math.random in javascript?

  18. 18

    how to find the appropriate math function for series with random numbers in Excel

  19. 19

    Creating an object of Random class or using Math.random() in order to generate random numbers

  20. 20

    How does the Math.max.apply() work?

  21. 21

    How does math.min actually work?

  22. 22

    How does ~~ work as math.floor?

  23. 23

    math.pow does not work in ApplicationCraft?

  24. 24

    How does Java.math.BigInteger.and() work?

  25. 25

    How does math.min actually work?

  26. 26

    PHP math does not work in sql value ZERO

  27. 27

    Javascript - Math.floor does not work

  28. 28

    Why isn't Math.floor((Math.random() * 999999) + 100000) reliable for producing 6 digit numbers?

  29. 29

    Code does not work for large numbers

HotTag

Archive