最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

Javascript: how to remove last comma in for-loop - Stack Overflow

matteradmin4PV0评论

How can I remove the last ma from this function.

   for(var i = 0; i <= 100; i++) { 
          if(i % 2 === 0) { 
            div.innerHTML += l + ","; 
          } 
         else { 
            div.innerHTML += " ";
         }
   } 

How can I remove the last ma from this function.

   for(var i = 0; i <= 100; i++) { 
          if(i % 2 === 0) { 
            div.innerHTML += l + ","; 
          } 
         else { 
            div.innerHTML += " ";
         }
   } 
Share Improve this question edited Oct 12, 2015 at 11:07 Kevbear asked Oct 12, 2015 at 10:15 KevbearKevbear 3611 gold badge4 silver badges10 bronze badges 4
  • Add this after the for loop, dividedThree.innerHTML = dividedThree.innerHTML.substring(0, dividedThree.innerHTML.length - 1); – Tushar Commented Oct 12, 2015 at 10:18
  • if (i % 2 === 0 && i !== 100) { ... ? – adeneo Commented Oct 12, 2015 at 10:18
  • in practice, you should avoid updating innerHTML in a loop like that – Jaromanda X Commented Oct 12, 2015 at 10:20
  • do you really add l? or is it i? with i you get 0, 2, 4, 6, ..., 98, 100,. the function is missing some (). – Nina Scholz Commented Oct 12, 2015 at 10:26
Add a ment  | 

4 Answers 4

Reset to default 6

First, use arrays instead strings for sum strings, its faster. Second:

var arr = [];
for (var i = 0 ;i <= 100; i+=2) arr.push(l);
dividedThree.innerHTML = arr.join(', ');
function loop {
    for (var i = 0; i <= 100; i++) {
        if (i % 2 === 0) {
            dividedThree.innerHTML += l;
            if (i < 100)
            {
                dividedThree.innerHTML += ",";
            }
        } else {
            dividedThree.innerHTML += " ";
        }
    }
}

Try using and (&&) condition for 100 and show only i value for 100.

function loop() {
  var dividedThree = document.getElementById('MY_ID');
  for (var i = 0; i <= 100; i++) {
    if (i % 2 === 0 && i !== 100) {
      dividedThree.innerHTML += i + ", ";
    } else if (i === 100) {
      dividedThree.innerHTML += i;
    } else {
      dividedThree.innerHTML += " ";
    }
  }
}
loop();
<p id="MY_ID">
  <p>

does this version work for you?

function loop() {
    var data = '';
    for (var i = 0; i <= 49; i++) { 
        data  += l + ',' + ' ';
    }
    data  += l;
    dividedThree.innerHTML = data;
}

What are you trying to do? where does 'l' e from?

Post a comment

comment list (0)

  1. No comments so far