0

Task is simple write numbers from 1 to 99 and replace all numbers where: num (mod 3) =0 with each num (mod 5) =0 -> mat, num ( mod 3=0 && mod 5=0 ) with each mat, all others remains the same. That is easy but the formatting is horrible so I want to do this: insert a space between every number and place every "special" word (dividable by 3,5 or both) on a separate line. I know I have to use string.prototype.format but how exactly? here is my current code:

<html>
<body>

<p>Click the button to join two strings into one new string.</p>

<button onclick="myFunction()">Try it</button>

<p id="demo"></p>

<script>


function myFunction() {
     var text = []; 
      var res = " ";   

     for (i=1; i<100; i++){
   if (i%3 == 0 && i%5 == 0){
        text[i]= "SachMat ";
                }
       else if (i%3 == 0){
        text[i]= 'sach ';
                }
        else if (i%5 == 0){
        text[i]= 'mat ';
                }

        else {
        text[i]= i;                
                }       
      var res = res.concat(text[i]);
       }


 document.getElementById("demo").innerHTML = res;
}
</script>

</body>

</html>

1 Answer 1

1

Try this

function myFunction() {
    var text = []; 
    var res = " ";   

    for (i=1; i<100; i++){
        if (i%3 == 0 && i%5 == 0){
            text[i]= "<br/>SachMat<br/>";
        } else if (i%3 == 0) {
            text[i]= 'sach ';
        } else if (i%5 == 0){
            text[i]= 'mat ';
        } else {
            text[i]= i;                
        }       
        res = res.concat(' ' + text[i]);
    }
    document.getElementById("demo").innerHTML = res;
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.