0

I'm trying to count how many different data types are in this array, var arr. The output I am trying to return should be 2.

var arr = [12, 45, 66, 88, true, false]
var count = [];

function check_types(arr) {
  for (i = 0; i <= typeof arr.length; i++) {}
  return count;
}
check_types(arr)

I appreciate the feedback that helps me notice what I did wrong and so that I avoid repeating mistakes in future code. Thank you.

3
  • This code doesn't make much sense actually. Your loop serves no purpose and typeof arr.length also makes no sense. Commented Aug 28, 2018 at 18:17
  • developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/… Commented Aug 28, 2018 at 18:17
  • 1
    so why do you have i <= "number". What you did wrong is you have no logic in your code that actually checks the type. Commented Aug 28, 2018 at 18:19

4 Answers 4

2

You could use an object as hash table for just setting a flag. The count the keys for used types.

var array = [12, 45, 66, 88, true, false],
    types = {};

array.forEach(v => types[typeof v] = true);

console.log(types);
console.log(Object.keys(types).length);

Sign up to request clarification or add additional context in comments.

1 Comment

Ok, the smartest answer
1

The output I am trying to return should be 2.

then why do you assign an array to count and not 0?

Additionally you do typeof arr.length which is "number" and it makes little sense to compare that to an index.


Now to get the number of unique types you could map the array to an array of types, then turn it into a unique Set and get its size:

 return new Set(arr.map(el => typeof el)).size;

Comments

1

This will do the trick

function check_types(arr) {
  var types = {};
  arr.forEach(function(i){
    if (types[typeof i] === undefined)
      types[typeof i] = 1;
    else
      types[typeof i]++;
  });
  return Object.keys(types).length;
}

var arr = [12, 45, 66, 88, true, false];

console.log(check_types(arr));

Creates an object where each key represents a type in the array, and the value of each key is the number of the that type within the array. You don't need these numbers right now, so you can just count the amount of keys in that object.

Comments

0

There are couple of mistakes, 1. you need to check data types of element inside array. type of array.length is always number 2. for array , you need to loop till arr.length(i< arr.length)

var arr = [12, 45, 66, 88, true, false]
var count = [];

function check_types(arr) {
  for (i = 0; i <arr.length; i++) {
   let dtype = typeof arr[i]
     if(!count.includes(dtype)) {
     count.push(dtype)
   }
  }
  return count.length;
}
console.log(check_types(arr))
console.log(count)

1 Comment

Lacks an explaination. Good answers have explanations on what is going on.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.