-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.
– André
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
orfalse
?– user168265