-
Notifications
You must be signed in to change notification settings - Fork 1
/
1179.js
34 lines (27 loc) · 1.2 KB
/
1179.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
// In this problem you need to read 15 numbers and must put them into two different arrays: par if the number is even or impar if this number is odd. But the size of each of the two arrrays is only 5 positions. So every time you fill one of two arrays, you must print the entire array to be able to use it again for the next numbers that are read. At the end, all remaining numbers of each one of these two arrays must be printed beggining with the odd array. Each array can be filled how many times are necessary.
const array = [1, 3, 4, -4, 2, 3, 8, 2, 5, -7, 54, 76, 789, 23, 98];
const evenNumbers = [];
const oddNumbers = [];
for (let i = 0; i < array.length; i++) {
const number = array[i];
if (number % 2 === 0) {
evenNumbers.push(number);
} else {
oddNumbers.push(number);
}
if (evenNumbers.length === 5) {
printNumbers("par", evenNumbers);
evenNumbers.length = 0;
}
if (oddNumbers.length === 5) {
printNumbers("impar", oddNumbers);
oddNumbers.length = 0;
}
}
printNumbers("impar", oddNumbers);
printNumbers("par", evenNumbers);
function printNumbers(type, numbers) {
for (let i = 0; i < numbers.length; i++) {
console.log(`${type}[${i}] = ${numbers[i]}`);
}
}