Random slogan generator with using javascript's switch

前端 未结 3 1950
一生所求
一生所求 2021-01-07 14:46

I have got little problem. I\'m learning javascript and I wanted to create random slogan generator for my site with using switch.

So I created this html

3条回答
  •  花落未央
    2021-01-07 15:27

    I would use an array instead of a switch statement for this, to make it more flexible. For example:

    var quotesList = ["I'm a great guy!", "Not even kidding.", "Just incredible."];
    
    var randQuote = function(quotes) {
        var choice = Math.floor(Math.random() * quotes.length);
        return quotes[choice];
    }
    
    document.getElementById("quote").innerHTML = randQuote(quotesList);
    

    This way, the size of the quote array can be changed freely without having to change any of the code.

    Demo: jsfiddle

提交回复
热议问题