最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - JS - How to start from new line inside for loop? - Stack Overflow

programmeradmin2浏览0评论

I am trying to print 6 random numbers after clicking a button. Then every time I click the button again, random numbers should start from new line however I do not know how. I tried everything and nothing works. I appreciate any help.

        function fname() {
            for(i=1; i<=6; i++) {
                    number = number + Math.floor(Math.random() * 47 + 1) + "-";
                    var print = number + " GOOD LUCK!";
            }
            document.getElementById("total").value = print;
        }
<!DOCTYPE html>
<html>
    <head>
        <title>Let's ROLL!</title>
        <meta charset="UTF-8">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <style>
            input, button {display: block;}
        </style>
        <script>
        var number = "";
        function fname() {
            for(i=1; i<=6; i++) {
                    number = number + Math.floor(Math.random() * 47 + 1) + "-";
                    var print = number + " GOOD LUCK!";
            }
            document.getElementById("total").value = print;
        }
        </script>
    </head>
    <body>
        <div>
            <button onclick="fname()">ROLL!</button>
            <textarea id="total" rows="12" cols="50" readonly></textarea>
        </div>
    </body>
</html>

I am trying to print 6 random numbers after clicking a button. Then every time I click the button again, random numbers should start from new line however I do not know how. I tried everything and nothing works. I appreciate any help.

        function fname() {
            for(i=1; i<=6; i++) {
                    number = number + Math.floor(Math.random() * 47 + 1) + "-";
                    var print = number + " GOOD LUCK!";
            }
            document.getElementById("total").value = print;
        }
<!DOCTYPE html>
<html>
    <head>
        <title>Let's ROLL!</title>
        <meta charset="UTF-8">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <style>
            input, button {display: block;}
        </style>
        <script>
        var number = "";
        function fname() {
            for(i=1; i<=6; i++) {
                    number = number + Math.floor(Math.random() * 47 + 1) + "-";
                    var print = number + " GOOD LUCK!";
            }
            document.getElementById("total").value = print;
        }
        </script>
    </head>
    <body>
        <div>
            <button onclick="fname()">ROLL!</button>
            <textarea id="total" rows="12" cols="50" readonly></textarea>
        </div>
    </body>
</html>

Share Improve this question asked Oct 26, 2015 at 16:39 mad.mixmad.mix 531 gold badge2 silver badges12 bronze badges 2
  • Isn't this just a matter of scope? print is being initialised inside the for loop then accessed outside. – MTCoster Commented Oct 26, 2015 at 17:15
  • @MTCoster, yes I made mistake by having almost all my code inside for loop. Thanks :) – mad.mix Commented Oct 26, 2015 at 17:31
Add a ment  | 

3 Answers 3

Reset to default 2

Not 100% clear on where you wanted the breaks, but in a text area, a line break is \n. If this was in an HTML element, you would use <br />.

var number = "";

function fname() {
  for (i = 1; i <= 6; i++) {
    number = number + Math.floor(Math.random() * 47 + 1) + "-";
  }
  number = number + "\n";
  
  var print = number + "GOOD LUCK!";
  document.getElementById("total").value = print;
}
 input,
 button {
   display: block;
 }
<div>
  <button onclick="fname()">ROLL!</button>
  <textarea id="total" rows="12" cols="50" readonly></textarea>
</div>

Add "\n".

I am assuming you want to concatenate the new text in the text area, so you should use += instead of =:

document.getElementById("total").value += print + "\n";

You can use arrays and .join() the numbers and lines together by their appropriate delimiters. This only inserts the characters between the elements. \n in a string renders a new line.

var button = document.getElementById('roll');
var total = document.getElementById('total');

var rolls = [];

button.onclick = function() {
    var roll = [];
    for(var i=0; i<6; ++i) roll.push(Math.floor(Math.random() * 47 + 1));
    rolls.push(roll.join('-'));
    total.value = rolls.join('\n') + "\nGOOD LUCK!";
}
<button id="roll">ROLL!</button><br>
<textarea id="total" rows="12" cols="50" readonly></textarea>

发布评论

评论列表(0)

  1. 暂无评论