I have arrays in javascript: arrayOne, arrayTwo, arrayThree, arrayFour... with variable numbers of data in each
Now the user gives input in input box One/Two/Three... and the index of the array.
For example, if the user inserted One and 3 as inputs my task would be to return the value from arrayOne[3]
I am unable to retrieve the data from array. My code returns the 3rd letter from string "arrayOne" ie. "a".
a= arrayOne
Index= 3
a[index] : a
How to retrieve the arrayOne[3] data?
var arrayOne = ['mango', 'banana', 'pine', 'orange'];
var arrayTwo = ['adobe', 'microsoft', 'apple', 'yahoo'];
var arrayThree = ['england', 'china', 'nepal', 'japan', 'usa', 'india'];
// suppose i have many more arrayFour,arrayFive,....,arrayFifty
function fun() {
var index = document.getElementById('index').value;
var name = document.getElementById('name').value;
var a = 'array' + name;
var b = a[index];
document.getElementById('para').innerHTML = b;
document.getElementById('para3').innerHTML = index;
document.getElementById('para2').innerHTML = a;
}
p {
border: 1px dotted black;
}
<!-- Try with One and 3 -->
Choose one,two,three,..,fifty:<br>
<input type='text' id='name'><br> Choose index:<br>
<input type='number' id='index'><br>
<input type='button' onclick='fun();' value='Go'>
<p>a=
<span id='para2'></span>
</p>
<p>Index=
<span id='para3'></span>
</p>
<p>
a[index] :
<span id='para'></span>
</p>
I have created the HTML,Js,Css in Fiddle on http://jsfiddle.net/wh0q21bL/