vendredi 16 octobre 2015

assigning values to multiple random numbers in javascript

I want to generate 15 random numbers in the range 1-18 and assign them values. Here is my code. The problem is, when I press the button, the text displayed by Javascript, is 15 random items from the list I gave, which is exactly what I want, however, these items replace the 'Scramble' button. I want the button to stay so that I can generate another random list without reloading the page.

<!DOCTYPE html>
<html>
<head>
<title>text/javascript</title>
<script language="javascript"> 
function myFunction() {
document.getElementById('Scramble');
for (var i = 0; i < 15; i++) {
var x = Math.floor((Math.random()*18)+1)
if (x === 1) {
    document.write("R ")
};
if (x === 2) {
    document.write("R' ")
};
if (x === 3) {
    document.write("R2 ")
};
if (x === 4) {
    document.write("L ")
};
if (x === 5) {
    document.write("L' ")
};
if (x === 6) {
    document.write("L2 ")
};
if (x === 7) {
    document.write("F ")
};
if (x === 8) {
    document.write("F' ")
};
if (x === 9) {
    document.write("F2 ")
};
if (x === 10) {
    document.write("B ")
};
if (x === 11) {
    document.write("B' ")
};
if (x === 12) {
    document.write("B2 ")
};
if (x === 13) {
    document.write("U ")
};
if (x === 14) {
    document.write("U' ")
};
if (x === 15) {
    document.write("U2 ")
};
if (x === 16) {
    document.write("D ")
};
if (x === 17) {
    document.write("D' ")
};
if (x === 18) {
    document.write("D2 ")
};
};
};
</script>
</head>
<body>
<button onclick="myFunction()">Scramble</button>
<p id="Scramble"></p>
</body>
</html>




Aucun commentaire:

Enregistrer un commentaire