mardi 31 mai 2016

Why does my random number generator always give me one?

I'm working on a project that will give me a random town wealth depending on what number is randomly generated. Yet whenever I press the "establish township" button, I always get "wealthy". How can I fix my code to enable desired results?

<!DOCTYPE html>
<html>
<body>
<style>
h1 {font-size: 20pt; color: red;}
    p {font-size: 17pt; color: blue;}
    p2 {font-size: 18pt; color: orange;}
    p3 {font-size: 18pt; color: green;}
</style>
<p>This program will create a random town upon the click of a button.</p>

<button onclick="numberdescription()">Establish Township</button>
<br /><br /><br />
<p3 id="random"></p3>

<script>

function numberdescription() {
var num = Math.floor(Math.random() * 3 + 1)
    if (num = 1) {
        desc = "wealthy";
    } else if (num = 2) {
        desc = "middle wealth";
    } else {
        desc = "dirt poor";
    }
document.getElementById("random").innerHTML = desc;
}
</script>

</body>
</html>




Aucun commentaire:

Enregistrer un commentaire