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

javascript - For loop inside my custom function doesn't work - Stack Overflow

programmeradmin2浏览0评论

I'm trying to create a function that uses the number input I got from users to create the same amount of division inside of a container division.However, no matter what the number is, it always creates only 1 div. It seems like the for loop inside my function is being avoided.

I've tried to alter the function, checked number input whether it is defined or undefined.

function createGrid(parameter) {
  for (i = 0; i < parameter * parameter; i++); {
    const div = document.createElement('div');
    newDiv = container.appendChild(div);
    newDiv.setAttribute('class', 'newDiv');
  }
  return newDiv;
}

I'm trying to create a function that uses the number input I got from users to create the same amount of division inside of a container division.However, no matter what the number is, it always creates only 1 div. It seems like the for loop inside my function is being avoided.

I've tried to alter the function, checked number input whether it is defined or undefined.

function createGrid(parameter) {
  for (i = 0; i < parameter * parameter; i++); {
    const div = document.createElement('div');
    newDiv = container.appendChild(div);
    newDiv.setAttribute('class', 'newDiv');
  }
  return newDiv;
}
Share Improve this question edited Sep 11, 2019 at 15:52 Andreas 21.9k7 gold badges51 silver badges58 bronze badges asked Sep 11, 2019 at 15:45 tosyntosyn 576 bronze badges 5
  • 1 What's container? – j08691 Commented Sep 11, 2019 at 15:47
  • Please provide a Minimal, Reproducible Example. – palaѕн Commented Sep 11, 2019 at 15:48
  • 6 Remove the semicolon ; after for loop. – Nikhil Commented Sep 11, 2019 at 15:50
  • Container is the id of my main division. I am trying to create divisions inside of this container division. That is why I'm using appendChild. – tosyn Commented Sep 11, 2019 at 15:51
  • Your code doesn't really make sense. const div is a new div you create. In the next line, you (re-)declare a global variable newDiv and set it to be container. Where is container defined? Then you apply the newDiv class to the container over and over again. – André Reichelt Commented Sep 11, 2019 at 15:51
Add a ment  | 

2 Answers 2

Reset to default 9

You have semicolon ; after for loop which is essentially an empty statement.

That is the reason the for loop is not working as expected, and rest of your code is just creating one divider.

Remove the semicolon ; to fix the issue.

Additional to Nikhil's answer, here is how I would write it (without using global variables, which is considered to be bad practice in most cases):

function createGrid(parameter) {
  let newDiv;
  for (let i = 0; i < parameter * parameter; i++) {
    newDiv = document.createElement('div');
    newDiv.setAttribute('class', 'newDiv');
    container.appendChild(newDiv);
  }
  return newDiv;
}

If you don't need to return the last div added, just remove the let newDiv; line and put the const keyword back into the first line of the for loop. Also remove the return value then.

function createGrid(parameter) {
  for (let i = 0; i < parameter * parameter; i++) {
    const newDiv = document.createElement('div');
    newDiv.setAttribute('class', 'newDiv');
    container.appendChild(newDiv);
  }
}
发布评论

评论列表(0)

  1. 暂无评论