-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathday-41.js
More file actions
99 lines (87 loc) · 3.13 KB
/
day-41.js
File metadata and controls
99 lines (87 loc) · 3.13 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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
const players = [
{name: 'Name 1', age: 18, score: 0, status: 0},
{name: 'Name 2', age: 10, score: 30, status: 1},
{name: 'Name 3', age: 15, score: 0, status: 1},
{name: 'Name 4', age: 15, score: 40, status: 0}
];
const names = players.map(item => item.name);
const scores = players.map(item => item.score);
// Includes
const includes = names.includes('Name 2');
console.log(`Name 2 is ${includes ? `included` : `not included` }`);
// Name 2 is included
// IndexOf
const indexOf = scores.indexOf(0);
console.log(`Position ${indexOf}`, players[indexOf]);
// Position 0 {id: 1, name: "Name 1", age: 18, score: 0, status: 0}
// LastIndexOf
const lastIndexOf = scores.lastIndexOf(0);
console.log(`Position ${lastIndexOf}`, players[lastIndexOf]);
// Position 2 {id: 3, name: "Name 3", age: 15, score: 0, status: 1}
// Find
const find = players.find(item => item.status === 1);
console.log(`First item found:`, find);
// First item found: {id: 2, name: "Name 2", age: 10, score: 30, status: 1}
// FindIndex
const checkAdult = item => item.age >= 18;
const findIndex = players.findIndex(checkAdult);
console.log(`First position found: ${findIndex}`);
// First position found: 0
const prompt = document.querySelector('[data-js="prompt"]');
const form = document.querySelector('[data-js="form"]');
const example = document.querySelector('[data-js="example"]');
const input = document.querySelector('[data-js="input"]');
input.focus();
form.addEventListener('click', () => {
input.focus();
});
input.addEventListener('input', evt => {
let text = '';
switch(evt.target.value) {
case 'includes':
text = `
// Includes
const includes = names.includes('Name 2');
console.log(\`Name 2 is ${includes ? `included` : `not included` }\`);
// Name 2 is included
`;
break;
case 'indexof':
text = `
// IndexOf
const indexOf = scores.indexOf(0);
console.log(\`Position ${indexOf}\`, players[indexOf]);
// Position 0 {id: 1, name: "Name 1", age: 18, score: 0, status: 0}
`;
break;
case 'lastindexof':
text = `
// LastIndexOf
const lastIndexOf = scores.lastIndexOf(0);
console.log(\`Position ${lastIndexOf}\`, players[lastIndexOf]);
// Position 2 {id: 3, name: "Name 3", age: 15, score: 0, status: 1}
`;
break;
case 'find':
text = `
// Find
const find = players.find(item => item.status === 1);
console.log(\`First item found:\`, find);
// First item found: {id: 2, name: "Name 2", age: 10, score: 30, status: 1}
`;
break;
case 'findindex':
text = `
// FindIndex
const checkAdult = item => item.age >= 18;
const findIndex = players.findIndex(checkAdult);
console.log(\`First position found: ${findIndex}\`);
// First position found: 0
`;
break;
default:
text = evt.target.value.indexOf('hemerson') >= 0 ? ' // Nerd Calistenico' : '';
}
prompt.innerHTML = evt.target.value;
example.innerHTML = text;
});