0

I have a problem with my code . I want to make a program that generates random numbers and multiplies them by 50 then adds 30 to them then removes the remaining decimals. I also want to show how these number are calculated by using <h1> tags. after the equal symbol I want to show the final number that is multiplied by 50 and added by 30 and the decimals are removed.

<html>
<head>
    <meta charset="UTF-8"/>
    <title>Random Numbers</title>
    <script>
        function start(){
            var num1 = Math.random();
            var num2 = Math.random();
            var num3 = Math.random();
            var num4 = Math.random();
            var num5 = Math.random();
            var num11 = num1.toFixed(3);
            var num22 = num2.toFixed(3);
            var num33 = num3.toFixed(3);
            var num44 = num4.toFixed(3);
            var num55 = num5.toFixed(3);
            /*var num111 = (num11 * 50);
            var num222 = (num22 * 50);
            var num333 = (num33 * 50);
            var num444 = (num44 * 50);
            var num555 = (num55 * 50);
            /* ---------------------------------------------- 
            var num111 = (num11) += 30;
            var num222 = (num22) += 30;
            var num333 = (num33) += 30;
            var num444 = (num44) += 30;
            var num555 = (num55) += 30;*/
            document.getElementById("show1").innerHTML=num11 + '×' + '50' + '+' + '30' + '=' + num11;
            document.getElementById("show2").innerHTML=num22 + '×' + '50' + '+' + '30' + '=' + num22;
            document.getElementById("show3").innerHTML=num33 + '×' + '50' + '+' + '30' + '=' + num33;
            document.getElementById("show4").innerHTML=num44 + '×' + '50' + '+' + '30' + '=' + num44;
            document.getElementById("show5").innerHTML=num55 + '×' + '50' + '+' + '30' + '=' + num55;
        }
    </script>
</head>
<body>
    <button id="btn" onclick="start()">Press the button</button><br/>
    <h1 id="show1"></h1>
    <h1 id="show2"></h1>
    <h1 id="show3"></h1>
    <h1 id="show4"></h1>
    <h1 id="show5"></h1>
</body>

HavelTheGreat
  • 3,299
  • 2
  • 15
  • 34
Mohammad
  • 77
  • 1
  • 3
  • 9

3 Answers3

1

Check this: http://jsfiddle.net/wc5wnby5/2/

Probably you need to do like below:

            document.getElementById("show1").innerHTML=num11 + '×' + '50' +    '+' + '30' + '=' + Math.round(num11*50+30);
            document.getElementById("show2").innerHTML=num22 + '×' + '50' + '+' + '30' + '=' + Math.round(num22*50+30);
            document.getElementById("show3").innerHTML=num33 + '×' + '50' + '+' + '30' + '=' + Math.round(num33*50+30);
            document.getElementById("show4").innerHTML=num44 + '×' + '50' + '+' + '30' + '=' + (num44*50+30);
            document.getElementById("show5").innerHTML=num55 + '×' + '50' + '+' + '30' + '=' + Math.round(num55*50+30);
Rakesh_Kumar
  • 1,442
  • 1
  • 14
  • 30
0

Use Math.round() for removing decimal part. Consider following code:

document.getElementById("show1").innerHTML=num11 + '×' + '50' +    '+' + '30' + '=' + Math.round(num11*50+30);
document.getElementById("show2").innerHTML=num22 + '×' + '50' + '+' + '30' + '=' + Math.round(num22*50+30);
document.getElementById("show3").innerHTML=num33 + '×' + '50' + '+' + '30' + '=' + Math.round(num33*50+30);
document.getElementById("show4").innerHTML=num44 + '×' + '50' + '+' + '30' + '=' + Math.round(num44*50+30);
document.getElementById("show5").innerHTML=num55 + '×' + '50' + '+' + '30' + '=' + Math.round(num55*50+30);

DEMO

Reference for more details.

Community
  • 1
  • 1
Manwal
  • 23,450
  • 12
  • 63
  • 93
0

If i right understood your problem:

function print(elemID, text){
  document.getElementById(elemID).appendChild(document.createTextNode(text));
}

var res;
print('show1', (res = Math.random()));
print('show2', (res = res * 50));
print('show3', (res += 30));
print('show4', (res = res.toFixed(0)));

fiddle

Kirill Pisarev
  • 844
  • 4
  • 11