javascript – Check whether a string exists in an array

Question:

Write a function that checks if the passed skill vector has the JavaScript skill and returns a boolean true/false whether it exists or not.

function temHabilidade(skills) {
 // código aqui
}
var skills = ["Javascript", "ReactJS", "React Native"];
temHabilidade(skills); // true ou false

Tip: To check whether an array contains a value, use the indexOf() method.

I did it like this: (but it's going wrong)

<script>
         let skills = ["JavaScript", "ReactJS", "Flutter","React Native"] 

         function temHabilidade(skills) {
                if (skills == "JavaScript") {
                    return true
                } else {
                    return false
                }

         }
         var resultado = temHabilidade(skills)
         console.log(resultado)

    </script>

Answer:

You need to search the array for its content, you can't buy an array with a potential element of it that will obviously give false, after all, not even the types of the two data are the same, one is a list of data and the other is the singular data.

There are several ways to do this, I used the ready includes() method.

function temHabilidade(skills) {
    return skills.includes("JavaScript");
}
let skills = ["JavaScript", "ReactJS", "Flutter","React Native"];
var resultado = temHabilidade(skills);
console.log(resultado);

I put it on GitHub for future reference .

Note that I've simplified it by not doing an if because it already returns true or false and doesn't need anything else. I also put ; because it works without it in most cases, but not in all and when you catch a situation that doesn't work you will be pretty lost, get used to doing it the right way every time, even when you don't need to (apparently the statement took this precaution).

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 without any need or gain, on the contrary, it's more expensive, you can use indexOf() :

function temHabilidade(skills) {
    return skills.indexOf("JavaScript") != -1;
}
let skills = ["JavaScript", "ReactJS", "Flutter","React Native"];
var resultado = temHabilidade(skills);
console.log(resultado);

When in doubt, consult the documentation, that's why I've put links for you.

If the exercise forbids using some ready-made function to better teach you how to think about the algorithm then you can do it:

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 it on GitHub for future reference .

In this case I scan the array with the for then getting item by item I compare if it's what you're looking for, if it ends with true , if it goes through the entire loop without ending, or if no item is true then it ends with false since you didn't find what you were looking for.

Scroll to Top