Ole Petersen
Ole Petersen

Reputation: 680

How to get the next entry for a fixed entry in an array in html

Assume we have the array

[a,b,c,d,e,f,g,h].

I want to write a function that randomly take a value in my array. I have already written this in JS and HTML.

My question is this: I want the previously and the next letter for the chosen one.

For example we get "e". Then I want a function that returns "d" and "f". How can I do this?

My code is below. (My js.file is named "nyNumber.js").

function GetValue() {
  var myarray = new Array("a", "b", "c", "d", "e", "f", "g", "h");

  var random = myarray[Math.floor(Math.random() * myarray.length)];
  //alert(random);
  document.getElementById("message").innerHTML = random;
}
<input type="button" id="btnSearch" value="Give me" onclick="GetValue();" />
<p id="message"></p>

<script src="myNumber.js"></script>

Upvotes: 0

Views: 117

Answers (5)

yogesh vaidya
yogesh vaidya

Reputation: 117

Considering a closed loop, left of first element is not present so this will give you last element as left of first element and right of last element is not present so this will give you first element as right of last element.

var myarray = new Array("a", "b", "c", "d", "e", "f", "g", "h");
var random;

document.getElementsByClassName("item")[0].addEventListener("click", function (){
  random = Math.floor(Math.random() * myarray.length);
  document.getElementsByClassName("itemValue")[0].innerHTML = myarray[random];
  document.getElementsByClassName("siblings")[0].removeAttribute("disabled");
})

document.getElementsByClassName("siblings")[0].addEventListener("click", function (){
  var left = myarray[ (random - 1 + myarray.length) % myarray.length ];
  
    var right= myarray[ (random + 1) % myarray.length ];
    
  document.getElementsByClassName("leftSiblingValue")[0].innerHTML = left;
  document.getElementsByClassName("rightSiblingValue")[0].innerHTML = right;
})
<button class="item">item</button>
<button class="siblings" disabled>siblings</button>

<p><span>left siblign : </span><span class="leftSiblingValue"><span></p>
<p><span>item : </span><span class="itemValue"></span></p>
<p><span>right sibling : </span><span class="rightSiblingValue"></span></p>

Upvotes: 2

pr1nc3
pr1nc3

Reputation: 8338

var myarray = new Array("a", "b", "c", "d", "e", "f", "g", "h");
var random = myarray[Math.floor(Math.random() * myarray.length)];
//alert(random);
var position = myarray.indexOf(random);
var previousElement= myarray[position-1];
var nextElement= myarray[position+1];
console.log(previousElement);
console.log(nextElement);

A simple solution is to get the position of your random value using indexOf function and just move your array pointer 1 position behind and 1 forward. This will give you the values you need.

Upvotes: 0

maha
maha

Reputation: 643

you should save the value of the random index in a variable to get to the other indices, but of course you should check if it has a before and after.

function GetValue()
{
   var myarray= new Array(a,b,c,d,e,f,g,h);
   var index = Math.floor(Math.random() * myarray.length);
   var random = myarray[index];
   //alert(random);
   document.getElementById("message").innerHTML=random;
   return index;
}

now you can get the value and get elements depending to it.

Upvotes: 1

DrevanTonder
DrevanTonder

Reputation: 727

use Array.prototype.indexOf MDN reference

var myArray = ["a","b","c","d","e"];

var value = myArray[Math.floor(Math.random() * myArray.length)];
console.log("First Value:", value);

var position = myArray.indexOf(value);

var next = myArray[position + 1];
console.log("Next Value:", next);

var before = myArray[position - 1];
console.log("Before Value:", before);

Upvotes: 0

Nina Scholz
Nina Scholz

Reputation: 386604

Assuming you have strings in your array, you could take just the index and an offset for getting the left and right values of a given random index.

Values of indices who are not in the array show up with undefined.

var array = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h'],
    random = Math.floor(Math.random() * array.length),
    left = array[random - 1],
    item = array[random],
    right = array[random + 1];
    
console.log(left || 'no left value', item, right || 'no right value');

Upvotes: 2

Related Questions