How to make an orderly list?

Asked

Viewed 229 times

0

I need to set any value, and this value should serve as the amount of numbers in an ordered list, where at each position of the list there should be the cumulative sum of 1 up to the position number

A tip on how to get help already, because I hit the net in search of line code that gives me a light.

Example:

inserir a descrição da imagem aqui

  • 6

    Welcome to the Sopt. It would be interesting for you to add in the question, what you have already tried to do to solve the problem, and tell at what point you are having doubts.

  • I think you can find help on that: http://answall.com/questions/8136/como-crio-um-sum%C3%A1rio-no-texto-com-algum-wysiwyg

  • Without seeing your code or part of it, it’s hard.

  • Hello Ivo. How do you have these numbers? in an array or in the already generated HTML.

1 answer

3

You can use an ordered list of html "ol" and the children would be inserted dynamically by a loop

const target = 20;
const ol = document.querySelector('ol');
let lastValue = 0;

for (let index = 1; index <= target; index++) {
  const li = document.createElement('li');
  lastValue = index + lastValue
  li.innerHTML = lastValue;

  ol.appendChild(li);

}
<body>

  <ol>
  </ol>

</body>

Browser other questions tagged

You are not signed in. Login or sign up in order to post.