. . .

Write the Javascript function to get the ‘n’th elements of the array

javascript
Please share the article
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
<!-- Bootstrap CSS -->
    <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.2.1/css/bootstrap.min.css" integrity="sha384-GJzZqFGwb1QTTN6wy59ffF1BuGJpLSa9DkKMp0DgiMDm4iYMj70gZWKYbI706tWS" crossorigin="anonymous">
    <title>'n'th element of the array</title>
</head>

<body>

 <div class="container">

        <h3>Write the javascript function to get the 'n'th element of the array?</h3>

        <hr />
		
		 
  <form class="form-signin" method="post">
  <h3 class="font-weight-bold text-dark">'n'th element of the array</h3>
  <div class="form-group row">
    <label for="email" class="col-sm-2 col-form-label my-3">element of the array</label>
    <div class="col-md-6">
      <input type="text" class="form-control flex-md-fill flex-sm-fill rounded-pill my-3" id="value" placeholder="Enter Number">
    </div>
	<button type="button" class="btn btn-outline-primary flex-md-fill flex-sm-fill m-auto rounded-pill" onClick="checkValue()">Check Value</button>
  </div>
</form>
<p id="answer" class="mt-3 font-weight-bold">Your Array Element is: </p>
</div>

</div>
<!-- Optional JavaScript -->

<script>
function checkValue() {

var array =[1,2,3,4,5,6,7,8,9,10];

var newArray=parseFloat(document.getElementById("value").value.trim());

document.getElementById("answer").innerHTML=array[newArray-1];

}

</script>


<!-- Optional JavaScript -->



<!-- jQuery first, then Popper.js, then Bootstrap JS -->
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script>

$(document).ready(function () {
  //called when key is pressed in textbox
  $(".form-control").keypress(function (e) {
     //if the letter is not digit then display error and don't type anything
     if (e.which != 8 && e.which != 0 && (e.which < 48 || e.which > 57)) {
        //display error message
        $("#errmsg").html("Digits Only").show().fadeOut("slow");
               return false;
    }
   });
});


</script>

Leave a Reply

Your email address will not be published. Required fields are marked *

16 + 18 =