Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

120
Vistas
checks the data type of array with loop and conditionals

i want to check the array for the same datatypes. i found other solutions here but i want to do it with for loop and if conditonals. Can someone please explain, why my code is not working:

thank you very much!

function sameDataType(array){
    const helper = []
    for(let i = 0; i < array.length; i++){
        let dataType0 = typeof(array[i])
        let dataType1 = typeof(array[i+1])
        if(dataType0 === dataType1){
            helper.push(1)
        } else {
            helper.push("not")
            }    
    }    
    for(let j = 0; j < helper.length; j++){
        if(helper[j] === helper[j+1]){
            return "same"
        } else {
            return "different"
        }
    }
}
console.log(sameDataType([1, 1, "string"]))

about 4 years ago · Juan Pablo Isaza
2 Respuestas
Responde la pregunta

0

Please use Array.map and Array.filter function.

First you get array of types.

And then filter it with removing the duplicated values.

Then check the length.

Like this.

function sameDataType1(array){
    return array.map(val => typeof val).filter((val, index, self) => self.indexOf(val) === index).length === 1 ? "same" : "different";
}
console.log(sameDataType1([1, 1, "string"]))

about 4 years ago · Juan Pablo Isaza Denunciar

0

Will try to improve upon your code only.

Firstly check if the array even has enough elements. If only one element, simply return same. Then make sure you run your loop till the correct indices. Notice you have i+1 & j+1 in your code. You do arr[arr.length+1], you are getting undefined.

Also check for the condition when you have only two elements.

function sameDataType(array){
if(array.length > 1 ) {
    const helper = []
    for(let i = 0; i < array.length - 1; i++){
        let dataType0 = typeof(array[i])
        let dataType1 = typeof(array[i+1])
        if(dataType0 === dataType1){
            helper.push(1)
        } else {
            helper.push("not")
            }    
    }    
 
    if(helper.length  === 1 ){
    if(helper[0] === 1) return "same";
    return "different";
    
    }
    for(let j = 0; j < helper.length-1; j++){
        if(helper[j] === 1 && helper[j] === helper[j+1]){
            return "same"
        } else {
            return "different"
        }
    }
}
return "same";
}
console.log(sameDataType([1, 1 , "string"]))
console.log(sameDataType([1, "string:" ]))
console.log(sameDataType(["string", "string" , "string"]))

about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda