check if array element has a letter

Asked

Viewed 220 times

-1

I need an algorithm that reads an array, and returns me if one of the items has a letter... if one of the items has a letter, I want it to return me "qualitative", if all the elements have only numbers, I want it to return me "quantitative"... I made a scheme of him to erase the comma and the point so that there is no confusion at the time d check if it has only numbers or letters if the user enters with decimal numbers using either dot or comma... but anyway, if anyone can help me I really appreciate... that code is running perfectly on Chrome’s Navigator, but when I try to use it on my college job at NODEJS, it does not rotate... a, and detail kk the elements of the vector will reach me all in string... does anyone know how to explain tmb the reason the code runs in the browser and in Node js not? From now on, I thank you

var meucarro = ["19.98", "993", "228", "opal"];

    function verIsNaN(vetorEntrada) {
        var vetor = vetorEntrada
        var er = /^[0-9]+$/;
        let resposta = "quantitativa"
        for (let i = 0; i < vetor.length; i++) {
            if (vetor[i].indexOf(","))
            vetor[i] = vetor[i].replace(/[,.]+/g, '');

            if ((er.test(vetor[i])) === false) {
                resposta = "qualitativo"

            };
        }
        console.log(resposta)

    }


    verIsNaN(meucarro);

1 answer

0


var meucarro = ["19,98", "993", "228", "opala"];

function verIsNaN(arr) {
    // Substitui vírgulas por pontos (Necessário para usarmos a função Number())
    let ar = arr.map(item => {
        if (item.indexOf(',') > -1) {
            return item.replace(',', '.');
        }
        return item
    });

    // Verifica se todos os elementos do array podem ser convertidos em Number
    // Caso um dos items não possa ser convertido, isto é, possui um caractere não numérico
    // O comando if recebe valor false
    if (ar.every(item => Number(item))) {
        return 'Quantitativo';
    }

    return 'Qualitativo';
}

console.log(verIsNaN(meucarro));

Browser other questions tagged

You are not signed in. Login or sign up in order to post.