3

I have function which randomly selects values from arrayVariable and assingn them to paragraph element p[i].

I want to copy the randomly selected values to another array b[i].

How do i do it?

function rvalue() {
  var arrayVariable = ['1', '2', '3', '4', '5']
  arrayLength = arrayVariable.length;

  ptags = document.getElementsByName("values");
  for (i = 0; i < ptags.length; i++) {
    ptags[i].innerHTML = arrayVariable[Math.floor(Math.random() * arrayLength)];

  }
}
p {
  font-size: 2xpx;
  color: #000000;
 
}
<body onload="rvalue()">
<p name="values"></p>
</div>
<p name="values"></p>
</div>
<p name="values"></p>
</div>

</div>

4
  • What's the problem with current code? can you explain expected output? Commented Sep 12, 2018 at 4:55
  • create another array [] and push each arrayVariable[Math.floor(Math.random() * arrayLength)] into it Commented Sep 12, 2018 at 4:56
  • it works, iwant to make another array of the randomly selected values Commented Sep 12, 2018 at 4:57
  • so what would be the output you want for the code you provided? Commented Sep 12, 2018 at 4:58

5 Answers 5

1

To insert text into an HTML element it is better to use textContent instead of innerHTML.

Create an array to push the textContent in each iteration:

function rvalue() {
  var array = [];
  var arrayVariable = ['1', '2', '3', '4', '5']
  arrayLength = arrayVariable.length;

  ptags = document.getElementsByName("values");
  for (i = 0; i < ptags.length; i++) {
    ptags[i].textContent = arrayVariable[Math.floor(Math.random() * arrayLength)];
    array.push(ptags[i].textContent);
  }
  console.log(array);
}
p {
  font-size: 20px;
  color: #000000;
}
<body onload="rvalue()">

  <p name="values"></p>
  <p name="values"></p>
  <p name="values"></p>

</body>

Please Note: Your HTML has no opening div element and font-size has invalid value (2xpx).

Sign up to request clarification or add additional context in comments.

Comments

0

Like this

function randomizeParagraphs() {
    var possibleValues = ['1', '2', '3', '4', '5'],
        createdValues = [], // Empty array to store created values.
        pTags = document.getElementsByName("values");

    for (var i = 0; i < pTags.length; i++) {
        var item = possibleValues[Math.floor(Math.random() * possibleValues.length)];
        ptags[i].innerHTML = item;
        createdValues.push(item);
    }

    // Log the created values.
    console.log(createdValues);
}

Comments

0

It will store value in tempArray by index, you can use Array.push for normal insert in replace of tempArray[index] = arrayVariable[index];

tempArray.push(arrayVariable[index]);

and if you may get duplicate value in array if you use .push method then just use this code to remove duplicate entry

tempArray= tempArray.filter(function(val, i){
  return tempArray.indexOf(val)== i; 
});

function rvalue() {
    var arrayVariable = ['1', '2', '3', '4', '5']
    arrayLength = arrayVariable.length;
    var tempArray = [];

    ptags = document.getElementsByName("values");
    for (i = 0; i < ptags.length; i++) {
        var index =
            Math.floor(Math.random() * arrayLength);
        ptags[i].innerHTML = arrayVariable[index];
        tempArray[index] = arrayVariable[index];

    }
    console.log(tempArray);
}
p {
  font-size: 2xpx;
  color: #000000;
 
}
<body onload="rvalue()">
<p name="values"></p>
</div>
<p name="values"></p>
</div>
<p name="values"></p>
</div>

</div>

Comments

0

Hey you can assign get values like this,

function rvalue() {
  var arrayVariable = ['1', '2', '3', '4', '5']
  arrayLength = arrayVariable.length;

  arrRandomGeneratedValue = [];
  ptags = document.getElementsByName("values");
  for (i = 0; i < 5; i++) {
    randomGeneratedValue = arrayVariable[Math.floor(Math.random() * arrayLength)];
    ptags[i].innerHTML = randomGeneratedValue;
    arrRandomGeneratedValue.push(randomGeneratedValue);

  }
 return arrRandomGeneratedValue;
}

rvalue();

Comments

0

well I dunno if I understood but if you want to save the random values use the push method in the new array variable that you want to use as storage.

  function rvalue() {
  var b = [];
 var arrayVariable = ['1', '2', '3', '4', '5']
 arrayLength = arrayVariable.length;

   ptags = document.getElementsByName("values");
   for (i = 0; i < ptags.length; i++) {
   ptags[i].innerHTML = arrayVariable[Math.floor(Math.random() * arrayLength)];
   b.push(ptags[i].innerHTML);  
 }
 }

I hope that is what you were looking for :P regards!

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.