Javascript Reference Guide

 
 
This guide contains useful Javascript tips.

rss feed






Switch and random.



Information
none


Code
    <script type="text/javascript" language="JavaScript"> 
    var num_quotes = 3;
    var rand_no = Math.ceil(num_quotes * Math.random());
    switch(rand_no) {
        case 1: my_output = "A hobby a day keeps the doldrums away.";
                break;
        case 2: my_output = "Eye contact is the best accessory.";
                break;
        case 3: my_output = "I can have oodles of charm when I want to.";
                break;  
        default: my_output = "Sorry, no quotes";
    }
    document.write(my_output);
 
    document.write("<br />");
    
    var num_quotes = 3;
    var rand_no = Math.floor(num_quotes * Math.random());
    switch(rand_no) {
        case 0: my_output = "A hobby a day keeps the doldrums away.";
                break;
        case 1: my_output = "Eye contact is the best accessory.";
                break;
        case 2: my_output = "I can have oodles of charm when I want to.";
                break;  
        default: my_output = "Sorry, no quotes";
    }
    document.write(my_output); 
    </script> 
 
Result