-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathEx6.js
56 lines (43 loc) · 1.44 KB
/
Ex6.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
/**Create an arrow function named: countItems that counts the number of integer numbers,
* decimal numbers and strings stored in an array. The countItems function returns the following object: */
const countIntegerNumbers = (array) => {
let count = 0;
for(let i = 0; i < array.length; i++){
if(typeof(array[i]) === "number" && array[i] % 1 === 0){
count++;
}
}
return count;
}
const countStrings = (array) => {
let count = 0;
for(let i = 0; i < array.length; i++){
if(typeof(array[i]) === "string"){
count++;
}
}
return count;
}
const countDecimalNumbers = (array) => {
let count = 0;
for(let i = 0; i < array.length; i++){
if(typeof(array[i]) === "number" && array[i] % 1 !== 0){
count++;
}
}
return count;
}
const countItems = (array) => {
const countOfIntegers = countIntegerNumbers(array);
const countOfStrings = countStrings(array);
const countOfDecimals = countDecimalNumbers(array);
return { numIntegers : countOfIntegers, numDecimals : countOfDecimals, numStrings : countOfStrings };
}
const main = () => {
let array = ["this", "is", 1, 3, 2.1, "a", "test"];
let result = countItems(array);
console.log("Number of Integers: " + result.numIntegers)
console.log("Number of Decimal Numbers: " + result.numDecimals)
console.log("Number of Strings: " + result.numStrings)
}
main();