Click here to Skip to main content
15,896,111 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I really have no idea on how to do this. A function that loops over an array and returns the number of different types of data.

//======================  EXAMPLE  ========================
check_types([1,5,9,33,65,122,66,['banana']])
2 // <======  EXPECTED OUTPUT
check_types([{},"hello", 55,22,333, "66"])
3 // <======  EXPECTED OUTPUT
//=========================================================



I thought that I would have to loop several times, one for each different type of value, and store in a variable how many elements of a type are in the array, then check the variables, and if any of them equals zero it means that data type isn't in the array, and for the ones that are in the array create a count that increases by one if that type of data appears in the array. So here is my code, but it doesn't work properly. There has to be a better way to do this. Thanks in advance.

What I have tried:

function check_types(array){
    var string = 0;
    var number = 0;
    var boolean = 0;
    var object = 0;
    var function1 = 0;
    var null1 = 0;
    var array1 = 0;
    var undefined1 = 0;
    var count = 0;
    for(let i = 0 ; i < array.length; i++) {
        if (typeof array[i] == "string"){
            string++;
        } else if (typeof array[i] == "number") {
            number++;
        } else if (typeof array[i] == "boolean") {
            boolean++;
        } else if (typeof array[i] == "object") {
            object++;
        } else if (typeof array[i] == "function") {
            function1++;
        } else if (typeof array[i] == "null") {
            null1++;
        } else if (typeof array[i] == "array") {
            array1++;
        } else if (typeof array[i] == "undefined") {
            undefined1++;
        }
    }
    if (string > 0){
        count++;
    } else if (number > 0){
        count++;
    } else if (boolean > 0){
        count++;
    } else if (object > 0){
        count++;
    } else if (function1 > 0){
        count++;
    } else if (null1 > 0){
        count++;
    } else if (array1 > 0){
        count++;
    } else if (undefined1 > 0){
        count++;
    } else {
        return "There are no values."
    }
    return count;
}
Posted
Updated 17-Aug-21 5:45am

1 solution

See typeof - JavaScript | MDN[^].

If you only need to count the total number then you only need a single counter. Set it to zero at the beginning of the loop, and increment it for every type that is found. The second if/else block is not needed.

[edit]
Something like the following is a simpler way:
JavaScript
function check_types(array) {
    var counts = [0, 0, 0, 0, 0, 0, 0, 0]; // keep the counts here

    for (let i = 0; i < array.length; i++) {
        if (typeof array[i] == "string") {
            counts[0] = 1;
        } else if (typeof array[i] == "number") {
            counts[1] = 1;
        } else if (typeof array[i] == "boolean") {
            counts[2] = 1;
        } else if (typeof array[i] == "object") {
            counts[3] = 1;
        } else if (typeof array[i] == "function") {
            counts[4] = 1;
        } else if (typeof array[i] == "null") {
            counts[5] = 1;
        } else if (typeof array[i] == "array") {
            counts[6] = 1;
        } else if (typeof array[i] == "undefined") {
            counts[7] = 1;
        }
    }
    total = 0;
    for (let i = 0; i < counts.length; i++) {
        total += counts[i];
    }

    return total;
}
 
Share this answer
 
v2
Comments
gabriel19971029 17-Aug-21 12:03pm    
But like, won't that add one for each element? Like, for example if there are two strings it will add 2n but there's only one type, no? I tried deleting the second if/else block but it just gives me undefined.
Richard MacCutchan 17-Aug-21 12:12pm    
Instead of counting the number of each type, just set it to 1 each time it is found. Then you just need to add the number of 1s to get the total.Something like:
        if (typeof array[i] == "string"){
            string = 1;
        } else if (typeof array[i] == "number") {
            number = 1;
// etc
gabriel19971029 17-Aug-21 12:48pm    
Oh thanks a lot! That makes a lot of sense! Thanks! I'll do that!
gabriel19971029 17-Aug-21 13:53pm    
I tried this:
function check_types(array){
for(let i = 0 ; i < array.length; i++) {
if (typeof array[i] == "string"){
var string = 1;
} else if (typeof array[i] == "number") {
var number = 1;
} else if (typeof array[i] == "boolean") {
var boolean = 1;
} else if (typeof array[i] == "object") {
var object = 1;
} else if (typeof array[i] == "function") {
var function1 = 1;
} else if (typeof array[i] == "null") {
var null1 = 1;
} else if (typeof array[i] == "array") {
var array1 = 1;
} else if (typeof array[i] == "undefined") {
var undefined1 = 1;
} else {
return "There are no values."
}
return parseInt(string + number + boolean + object + function1 + null1 + array1 + undefined1);
}
}

But I just get NaN. I don't understand, I used parseInt, it should be a number.
Richard MacCutchan 17-Aug-21 15:34pm    
Why? I gave you a fully working example, so go through it line by line to see exactly what it is doing. As an aside you should go and study the documentation for parseInt() - JavaScript | MDN[^].

You also need to learn and understand the concept of scope.

I gave you a link yesterday to the W3Schools Javascript tutorial. You will learn much better, and faster, by working through that, instead of trying to learn by posting questions here.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900