You need to look in array by content, can not buy a array with a potential element of it that will obviously give false, after all not even the type of the two data are equal, one is a list of data and the other is the singular data.
There are several ways to do this, I used the ready method includes()
.
function temHabilidade(skills) {
return skills.includes("JavaScript");
}
let skills = ["JavaScript", "ReactJS", "Flutter","React Native"];
var resultado = temHabilidade(skills);
console.log(resultado);
I put in the Github for future reference.
Note that I simplified by not making one if
because it already returns true or false and does not need anything more than that. I also put ;
because it works without in most cases, but not at all and when you catch a situation that does not work will be well lost, get used to doing the right way always, even when you do not need (apparently the statement took this care).
If you’re going to use the hint, which I don’t think is good because, as you can see in the code, it needs two operations with no need or any gain, on the contrary, it’s more expensive, you can use the indexOf()
:
function temHabilidade(skills) {
return skills.indexOf("JavaScript") != -1;
}
let skills = ["JavaScript", "ReactJS", "Flutter","React Native"];
var resultado = temHabilidade(skills);
console.log(resultado);
When you have questions check the documentation, so I put links for you.
If the exercise prohibits using some function ready to teach better how to think about the algorithm then you can do:
function temHabilidade(skills) {
for (const item of skills) if (item == "JavaScript") return true;
return false;
}
let skills = ["JavaScript", "ReactJS", "Flutter","React Native"];
var resultado = temHabilidade(skills);
console.log(resultado);
I put in the Github for future reference.
In that case I swept the array with the for
then picking item by item I compare if that’s what you’re looking for, if that’s what it ends with true
, if he goes through the whole loop without closing, or if no item is true then he closes with false
since you didn’t find what you were looking for.