June 25, 2020
How to Find Middle Element in Array in Javascript
In this video tutorial, you will learn how to find middle element in array in javascript.
HTML:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <meta http-equiv="X-UA-Compatible" content="ie=edge"> <link rel="stylesheet" href="style.css"> <title>Document</title> </head> <body> <div> <button>Get</button> <h1>Result</h1> </div> <script src="script.js"></script> </body> </html> |
CSS:
1 2 3 4 5 6 7 8 9 10 11 12 |
body { text-align: center; } div { display: inline-block; } button { display: inline-block; padding: 10px 20px; } |
Javascript:
1 2 3 4 5 6 7 8 9 10 11 |
let btnGet = document.querySelector('button'); let result = document.querySelector('h1'); let users = ['James', 'Marks', 'John', 'Mary', 'Ronald']; btnGet.addEventListener('click', () => { let output = (users.length - 1) / 2; result.innerText = users[output]; }); |