Click here to Skip to main content
15,895,667 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Could anyone tell me where do I go wrong? Why isn't my code working? I´m a total beginner (as you can see) and I´ve been trying to understand what´s wrong for like 4 days already. I just can' t see it. It´s probably something obvious though..
Random number , 10 in a row.

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
    
</head>
<body>
	
	<div id="answer"></div>
 <script>       
   
     function answer () {
       
      var text = "" ;
      var number;
       
        
         for (var i = 1; i <= 100 ; i++) { 
    var number = Math.round((Math.random() *9998) + 1); 
          text +=  number ;
             
         } if (i % 10 == 0){ 
                text = text +  "<br>";     
           
 }           
    document.getElementById("answer") = text;
              
     }
</script> 
</body>
</html>


What I have tried:

A LOT. I´ve tried to change the code in different ways, tried to move var number into the loop and out of the loop. Now I´m just lost. I´ve been searching for answers all over the internet , but there is nothing that helps me EXACTLY the way I need.
Posted
Updated 3-Feb-21 21:56pm

1 solution

First off, we have no idea exactly what you are planning on doing, but that code has so many things wrong with it that it doesn't look like you thought about it before you started to write it.
1) Fix the indenting - it makes it a load easier to read and work out what is supposed to happen!
2) Your if condition is outside the loop, instead of inside - so it only gets executed after the loop is finished.
3) Your numbers aren't separated by anything, so even if they worked, you couldn't tell what values you generated.
4) You don't put the result string anywhere useful.
5) You don't call the function, so it never gets executed anyway!

Try this:
HTML
<!DOCTYPE html>
<html>
    <head>
        <meta charset="UTF-8">
    </head>
    <body>
        <div id="answer"></div>
        <script>       
            function answer () {
               var text = "" ;
               var number;
               for (var i = 1; i <= 100 ; i++) { 
                  var number = Math.round((Math.random() *9998) + 1); 
                  text +=  number ;
                  text += ", ";
                  if (i % 10 == 0){ 
                     text = text +  "<br>";     
                  }
               }           
               document.getElementById("answer").innerHTML = text;
            }
            answer();
        </script> 
    </body>
</html>
 
Share this answer
 
Comments
CKh991 4-Feb-21 4:30am    
Thank you so much for the feedback & tips! I would have never thought that I need to use that answer(); in the end. It´s new to me (like mostly everything)
OriginalGriff 4-Feb-21 4:56am    
Do yourself a favour and either go on a course, or get a book (Addison Wesley, Wrox, and Microsoft Press all do some good ones) and learn how to do this properly.
Your current method or learning isn't working, and you do need to learn this stuff in a structured way - guessing and youtube videos are a very bad way to learn and you will waste a humungous amount of time and effort to get nowhere!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900