I’m trying to do a JavaScript challenge here, I was wondering if someone can guide me as I am very new to JS:
function diffTypes(arr){
}
Here is my attempt:
const diffTypes = arr => {
let int = 0;
let string = 0;
let boolean = 0;
let array = 0;
let object = 0;
for (let i = 0; i < arr.length; i++){
if (typeof arr[i] === 'number'){
int += 1
} else if(typeof arr[i] === 'string') {
string += 1
} else if(typeof arr[i] === 'boolean') {
boolean += 1
} else if(typeof arr[i].constructor() === '[]') {
array += 1
} else if(typeof arr[i] === 'object') {
object += 1
}
}
let totalTypes = {
string: string,
array: array,
boolean: boolean,
object: object,
number: int
}
return totalTypes
}
const ar = [
1,
"str",
false,
{ name: 'Peter', age: 30 },
["a", "e", "i", "o", "u"],
]
console.log(diffTypes(ar))
However I am getting { string: 1, array: 0, boolean: 1, object: 2, number: 1 }
The end goal is to achieve this result:
console.log(diffTypes(ar))// => { string: 1, array: 1, boolean: 2, object: 1, number: 1 }
I am aware that typeof(arr) will = ‘Object’ so I tried to find different ways to += array.