-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMy11thJSExerciseCapturingData.js
More file actions
52 lines (37 loc) · 2.44 KB
/
My11thJSExerciseCapturingData.js
File metadata and controls
52 lines (37 loc) · 2.44 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
/*Task-1: Navigate in your browser to https://exampledomain.github.io/capture-data/index.html or http://www.example.com you will see a webpage with a simple message:
Example Domain
This domain is established to be used for illustrative examples in documents. You may use this domain in examples without prior coordination or asking for permission.*/
/*Task-2: Use the document.querySelector() method to query the h1 element on the page and assign it to the variable named h1.*/
const h1 = document.querySelector('h1');
/*Task-3: Declare a new variable, name it arr, and save the following array into it:
[
'Example Domain',
'First Click',
'Second Click',
'Third Click'
]*/
let arr = ['Example Domain', 'First Click', 'Second Click', 'Third Click'];
/*Task-4: Write a new function declaration, named handleClicks. It should not accept any parameters.
Inside of it, code a switch statement, and pass a single parameter to it, h1.innerText.
The body of the switch statement should have a total of 4 cases (the fourth being the default case).
The first case should start with case arr[0]:. It should set the h1.innerText to arr[1]. In other words, it should assign the value of arr[1] to the h1.innerText property. The next line should have only the break keyword.
The second case should start with case arr[1]:. It should set the h1.innerText to arr[2]. In other words, it should assign the value of arr[2] to the h1.innerText property. The next line should have only the break keyword.
The third case should start with case arr[2]:. It should set the h1.innerText to arr[3]. In other words, it should assign the value of arr[3] to the h1.innerText property. The next line should have only the break keyword.
The default case should set the value of the h1.innerText property to arr[0].*/
function handleClicks() {
switch(h1.innerText) {
case arr[0]:
h1.innerText = arr[1];
break;
case arr[1]:
h1.innerText = arr[2];
break;
case arr[2]:
h1.innerText = arr[3];
break;
default:
h1.innerText = arr[0];
}
}
/*Task-5: Add an event listener to which you've created an h1 variable in Task 2. Now, use that variable to run the addEventListener() method on it. Pass two arguments to the addEventListener() method: 'click' and handleClicks.*/
h1.addEventListener('click', handleClicks);