Aniket Narvankar

Aniket Narvankar

  • 559
  • 2.1k
  • 605.4k

Array

Jul 27 2015 1:59 AM
I have a array in javascript below is the script which i have written
script type="text/javascript" language="javascript">
var arr = [];
var count = 4;
var array1 = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9];
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="WebApplication14.WebForm1" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title></title>
<script src="Scripts/jquery-1.7.2.min.js" type="text/javascript"></script>
<script src="Scripts/jquery-1.8.2.min.js" type="text/javascript"></script>
<script type="text/javascript" language="javascript">
var arr = [];
var count = 4;
var array1 = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9];
function Validate() {
var a = array1[~ ~(Math.random() * array1.length)];
if (arr.length < count)
{
arr.push(a);
alert(arr);
}
else {
arr.splice(0,1);
arr.push(a);
alert(arr);
}
}
</script>
</head>
<body>
<div>
<p id="demo">
</p>
</div>
<button onclick="Validate()">Test</button>
</body>
</html>
array1 is the array which contains 10 elements 0 to 9,i have taken a variable 'a' to which I am assigning random value from array1 on button click,
arr is the array which is used for displaying last 4 data i.e. the random number which comes in 'a'.I have set count to 4,so first it checks length of array arr if it is less than 4,then adds elements to arr.when it is greater than 4,it removes first element of arr and adds the generated random number to last.everything is working fine.but while displaying the elements they are displayed in format 2,4,5,4 i want to displaythem in format 2-4-5-4 how should i do the same.Please do let me know about it.  
 
 

Answers (8)