Difficulty working with arrays

Asked

Viewed 52 times

-1

I have this code that returns normally using the instruction switch

function getLetter(s) {

    switch(true) {
        case 'aeiou'.includes(s):
            console.log("A");
            break;
        case 'bcdfg'.includes(s):
            console.log("B");
            break;
        case 'hjklm'.includes(s):
            console.log("C");
            break;
        case 'npqrstvwxyz'.includes(s):
            console.log("D");
            break;
    }
}
getLetter('i'); //Retorna A
getLetter('f'); //Retorna B
getLetter('k'); //Retorna C
getLetter('p'); //Retorna D

I did that same function only using arrays

function getLetter(s) {

    const A = ['a', 'e', 'i', 'o', 'u'];
    const B = ['b', 'c', 'd', 'f,', 'g'];
    const C = ['h', 'j', 'k', 'l', 'm'];
    const D = ['n', 'p', 'q', 'r', 's', 't', 'v', 'w', 'x', 'y', 'z'];
    if(s == A.includes(s)){
        console.log("A");
    } else if (s == B.includes(s)) {
        console.log("B");
    } else if(s == C.includes(s)) {
        console.log("C");
    } else {
        console.log("D");
    }

}
/*Tentei dessa forma bastante semelhante, porém
  passando conchetes e continua retornando a letra D

    if(s == A.includes([s])){
        console.log("A");
    } else if (s == B.includes([s])) {
        console.log("B");
    } else if(s == C.includes([s])) {
        console.log("C");
    } else {
        console.log("D");
    }
*/

//Não passa em nenhuma das condições
getLetter('i'); //Retorna D 
getLetter('f'); //Retorna D
getLetter('k'); //Retorna D
getLetter('p'); //Retorna D

I researched if the method really existed includes in array, and yes normally according to the mdn documentation, then why when will I make the comparison of s == A.includes(s) or s == A.includes([s]) returns D?

For more details of what the exercise is asking to access the site hackrank

  • A.includes([s]) returns true or false, you are asking if s equals true or false, it will always give D that way.

  • Okay, more then what’s the difference between https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/contains for this https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/includes and they both return true or false?

1 answer

1


You don’t need to compare s == A.includes([s]) just use A.includes(s), because the includes is already a condition of comparison.

The includes functions as a contains. How: The Set A contains s ... if this condition is true, you will enter the informed condition.

See below working?

function getLetter(s) {

    const A = ['a', 'e', 'i', 'o', 'u'];
    const B = ['b', 'c', 'd', 'f,', 'g'];
    const C = ['h', 'j', 'k', 'l', 'm'];
    const D = ['n', 'p', 'q', 'r', 's', 't', 'v', 'w', 'x', 'y', 'z'];
    if(A.includes(s)){
        console.log("A");
    } else if (B.includes(s)) {
        console.log("B");
    } else if(C.includes(s)) {
        console.log("C");
    } else {
        console.log("D");
    }

}
/*Tentei dessa forma bastante semelhante, porém
  passando conchetes e continua retornando a letra D

    if(s == A.includes([s])){
        console.log("A");
    } else if (s == B.includes([s])) {
        console.log("B");
    } else if(s == C.includes([s])) {
        console.log("C");
    } else {
        console.log("D");
    }
*/

//Não passa em nenhuma das condições
getLetter('i'); //Retorna D 
getLetter('f'); //Retorna D
getLetter('k'); //Retorna D
getLetter('p'); //Retorna D

Browser other questions tagged

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