How to replace an index with a String

1

I'm listing numbers from 1 to 15 and checking if the number is divisible by 3 and 5 by adding a text when true, so far it works, but only that it still displays the number under the text, how do I change the number for the text. I already used replace without success:

var nums = $(".nums");
	
  for(var i=1; i<16; i++) {

     if(i % 3 == 0) {
         var tres = i;
         tres = "Divisível por 3";
         nums.append(tres+"<br>");
     } else if(i % 5 == 0) {
         var cinco = i;
         cinco = "Divisível por 5";
         nums.append(cinco+"<br>");
     }

  nums.append(i+"<br>");

}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script><divclass="nums"></div>
    
asked by anonymous 25.09.2018 / 18:04

1 answer

2

If you store in an array you will have more flexibility to treat / precess the data the way you want, in this case include a <br> separator between the elements

var nums = [];
for(var i=1; i<16; i++) {

    if(i % 3 == 0 && i % 5 == 0)
        nums.push(i+ " é divisível por 3 e por 5");
    else if(i % 3 == 0)
        nums.push(i+ " é divisível por 3");
    else if(i % 5 == 0) 
        nums.push(i+ " é divisível por 5");
    else
        nums.push(i);

}
$(".nums").append(nums.join('<br>'));
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script><divclass="nums"></div>
    
25.09.2018 / 18:10