Want To Know How To Store Values In Array Element

Asked By 0 points N/A Posted on -
qa-featured

I want to store values in the array element, but I don’t know how to perform this particular task as it seems difficult to me. How to store values in array element?

SHARE
Answered By 0 points N/A #306073

Want To Know How To Store Values In Array Element

qa-featured

You can store values in array element through the array name and appropriate indices to specify the element individually. You have to follow some simple steps to store values in array –

1.) Specify the array name on the left of ‘=’ sign.

2.) Include the eXPression for each index you mention for every element you want to store. You need one index for each array element.

Answered By 590495 points N/A #310556

Want To Know How To Store Values In Array Element

qa-featured

Here’s an example how to store values in an array using JavaScript. This example is the one I used on my site to automatically redirect a page to a predefined set of URLs. I’ve shortened the array to five and change the actual addresses to just “URL”.

<script type=”text/javascript”>
var footSet = new Array();
footSet[1] = “URL”;
footSet[2] = “URL”;
footSet[3] = “URL”;
footSet[4] = “URL”;
footSet[5] = “URL”;

var min = 1;
var max = 5;
var get_seed = Math.floor(Math.random() * (max – min + 1)) + min;

function redirect() {
window.location=footSet[get_seed];
}

setTimeout(‘redirect()’, 10000);
</script>

In this example, there are five arrays stored in the variable labeled “footSet” and every array contains a URL. The “get_seed” variable contains the formula that will generate a random number between one and five. The minimum and maximum value that will be generated is controlled by the variables “min” and “max”. The random value that is generated will be stored in the variable “get_seed”.

When a value is generated, the “setTimeout” command will call the function called “redirect()” after ten seconds (10000). “window.location” will fire up and calling the variable “footSet” bearing the array number “get_seed”.

Related Questions