I need a sequential generator in javascript

1

I need a code that I put the URL and the number of repetitions in the input and when I click to generate the sequence exit like this in the textarea:

rede[0]="http://www.siteescolhidoaqui"; rede[1]="http://www.siteescolhidoaqui"; rede[2]="http://www.siteescolhidoaqui"; rede[3]="http://www.siteescolhidoaqui"; rede[4]="http://www.siteescolhidoaqui"; rede[5]="http://www.siteescolhidoaqui"; 

This way above starting at zero until the number chosen by me and always the same url chosen ... javascript and a mini form in html to put the URL and the number of repetitions ... I believe it's easy, can anyone help me?

I tried the one that renan commented and I managed:

<script>
(function() {

  document.querySelector('button').addEventListener('click', toTextarea);

  function toTextarea() {
    var $urlInput = document.querySelector('input[type="url"]'),
        url       = $urlInput.value,
        inputName = $urlInput.getAttribute('name'),
        repeat    = document.querySelector('input[type="number"]').value;

    if (url && repeat) {

      var output = '';
      for (var i = 0; i < repeat; i++)
        output += inputName + '[' + i + ']="' + url + '"; ';
      document.querySelector('textarea').textContent = output;
    }
  }
})();
</script>
<input placeholder='URL' type='url' name='rede'>
<input placeholder='Sequencia' type='number'>
<button>Ir</button>

<textarea placeholder='Resultado'></textarea>
    
asked by anonymous 11.04.2016 / 06:36

1 answer

3

It was not clear if you should consider the sequence or the number of elements. For example, 5 repetitions based on the quantity:

[0][1][2][3][4] = 5 elementos.

Or, 5 reps considering the index:

[0][1][2][3][4][5] = 6 elementos.

In case of being second, just change the loop below to go from 0 until i is <= the value of repetitions:

(function() {

  document.querySelector('button').addEventListener('click', toTextarea);

  function toTextarea() {
    var $urlInput = document.querySelector('input[type="url"]'),
        url       = $urlInput.value,
        inputName = $urlInput.getAttribute('name'),
        repeat    = document.querySelector('input[type="number"]').value;

    if (url && repeat) {

      var output = '';
      for (var i = 0; i < repeat; i++)
        output += inputName + '[' + i + ']="' + url + '"; ';
      document.querySelector('textarea').textContent = output;
    }
  }
})();
<input placeholder='URL' type='url' name='rede'>
<input placeholder='Sequência' type='number'>
<button>Ir</button>

<textarea placeholder='Resultado'></textarea>
    
11.04.2016 / 08:02