-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex-4.html
More file actions
202 lines (180 loc) · 4.45 KB
/
Copy pathindex-4.html
File metadata and controls
202 lines (180 loc) · 4.45 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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta name="keywords" content="Arrays">
<title>Coding Assisement</title>
<link rel="stylesheet" href="./style.css">
</head>
<body>
<h2>4. Arrays</h2>
<!-- ====== write code -->
<div class="Que">
<p>Que.1 Find the Product.</p>
<pre>
const Find_Prod = (array, N) =>
{
let Prod=1; // declare Prod and initialize with 1
for(let i=0; i<N; i++){
Prod *=array[i]; // Prod= 1x1x2x3x4x5= 120
}
return Prod; // 120
};
</pre>
<p>Que.2 Find the Sum.</p>
<pre>
const Find_Sum = (array, N) =>
{
let sum = 0;
for(let i=0; i<N; i++){
sum = sum + array[i];
}
return sum;
};
</pre>
<p>Que.3 Count Occurrences</p>
<pre>
const findCount = (N, K, Arr) =>
{
let count = 0;
for(let i=0; i<N; i++){
if(Arr[i]===K){
// if array element value and data type is equal to k then increase count value
count++;
}
}
return count; // return final result of count
};
</pre>
<p>Que.4 Even Odd</p>
<pre>
const findEvenOdd = (N, Arr) =>
{
let Odd = 0; // Declare and initilize with 0 of Odd
let even = 0; // Declare and initilize with 0 of even
for(let i=0; i<N; i++){ // Now check every array element
if(Arr[i]%2===0){ // for even element
even += Arr[i];
}
else{
Odd += Arr[i]; // for odd element
}
}
return [even, Odd]; // final result sum of odd number's and even number's
};
</pre>
<p>Que.5 Find whether the number is present or not</p>
<pre>
const Find_Num = (array,N,M) =>
{
for(let i=0; i<N; i++){
if(array[i]===M){
return "YES";
}
}
return "NO";
};
</pre>
<p>Que.6 Higher Age</p>
<pre>
const highAge = (N, Arr) =>
{
const answer =[]; // other array
for(let i in Arr){
if(Arr[i]>=18){
answer.push(Arr[i]);
}
}
return answer;
};
</pre>
<p>Que.7 Increment the Array Elements</p>
<pre>
const Inc_Arr = (array,N) =>
{
const Ans = array.map(x => x + 1); // map is a built-in method of an array
// x is current array and x + 1 is increase 1 of array's per value
console.log(Ans.join(" "));
};
</pre>
<p>Que.8 Pass or Fail</p>
<pre>
const isAllPass = (N, Arr) =>
{
for(let i=0; i < N; i++){
for(let j=i+1; j < N; j++){
if(Arr[i]>=32 && Arr[j]>=32){
return "YES";
}
else{
return "NO";
}
}
}
return "No";
};
</pre>
<p>Que.9 Unique Color Shirt</p>
<pre>
function Unique_Shirts (arr,N) {
const num = [];
let Count = 0; // count of unique colors
for (let i = 0; i < N; i++) {
const color = arr[i];
num[color] = (num[color] || 0) + 1;
}
// count the number of unique colors
for (let color in num) {
if (num[color] === 1) {
Count++;
}
}
return Count;
}
</pre>
<p>Que.10 Min and Max</p>
<pre>
function arrayMin(arr) {
let minVal = arr[0];
for (let i = 1; i < arr.length; i++) {
if (arr[i] < minVal) {
minVal = arr[i];
}
}
return minVal;
}
function arrayMax(arr) {
let maxVal = arr[0];
for (let i = 1; i < arr.length; i++) {
if (arr[i] > maxVal) {
maxVal = arr[i];
}
}
return maxVal;
}
</pre>
<p>Que.11 Birthday Game</p>
<pre>
function Birthday_Game(arr,D ,M) {
let count = 0;
for (let i = 0; i <= arr.length - M; i++) {
let sum = 0;
for (let j = i; j < i + M; j++) {
sum += arr[j];
}
if (sum === D) {
count++;
}
}
return count;
}
</pre>
</div>
<div class="container">
<div class="row"><a href="./index-3.html">Back</a></div>
<div class="row"><a href="./index-5.html">Next</a></div>
</div>
</body>
</html>