-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
113 lines (94 loc) · 2.59 KB
/
index.js
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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
// Import stylesheets
import "./style.css";
// Write Javascript code!
//I am hacker
var array = null;
var array2 = null;
var max = 100;
document.getElementById("size").oninput = function() {
var input = document.getElementById("size").value;
if (input && input <= 100) {
array = [];
array2 = [];
for (var i = 0; i < input; i++) {
array[i] = Math.floor(Math.random() * max);
array2[i] = Math.floor(Math.random() * max);
}
} else {
array = null;
array2 = null;
}
printArrays(array, array2);
};
function printArrays(arr, arr2) {
document.getElementById("generatedarray1").innerHTML = "";
document.getElementById("generatedarray2").innerHTML = "";
if (arr && arr2) {
var common = find(arr, arr2);
console.log(common);
for (var i = 0; i < arr.length; i++) {
if (isCommon(arr[i], common)) {
document.getElementById("generatedarray1").innerHTML +=
"<div class='element common'> <span class='value'>" +
arr[i] +
"</span><span class='index'>" +
i +
"</span></div>";
} else {
document.getElementById("generatedarray1").innerHTML +=
"<div class='element'> <span class='value'>" +
arr[i] +
"</span><span class='index'>" +
i +
"</span></div>";
;
}
}
for (let i = 0; i < arr2.length; i++) {
if (isCommon(arr2[i], common)) {
document.getElementById("generatedarray2").innerHTML +=
"<div class='element common'> <span class='value'>" +
arr2[i] +
"</span><span class='index'>" +
i +
"</span></div>";
;
} else {
document.getElementById("generatedarray2").innerHTML +=
"<div class='element'> <span class='value'>" +
arr2[i] +
"</span><span class='index'>" +
i +
"</span></div>";
;
}
}
}
}
function find(arr, arr2) {
var common = [];
for (var i = 0; i < arr.length; i++) {
for (var g = 0; g < arr2.length; g++) {
if (arr[i] == arr2[g]) {
common.push(arr[i]);
;
}
}
}
return common;
}
function isCommon(ele, common) {
var isPresent = false;
for (var i = 0; i < common.length; i++) {
if (ele == common[i]) {
isPresent = true;
}
}
return isPresent;
}
function clearArray(){
document.getElementById("size").value = "";
document.getElementById("generatedarray1").innerHTML = "";
document.getElementById("generatedarray2").innerHTML = "";
}
document.getElementById("clear").onclick = clearArray;