Check if a string exists in an array

Asked

Viewed 1,189 times

4

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

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

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

I did it like this:(but it’s gone 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>

3 answers

10

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.

9

Cara is a very simple exercise, I will leave the solution, but be sure to try other ways to improve your prospects and Skills;

Try it this way:

function temHabilidade(skills) {
  return skills.indexOf("Javascript") >= 0;
}

let skills = ["Javascript", "ReactJS", "React Native"];
console.log(temHabilidade(skills));

0

Some examples of functions for the same exercise:

var skills = ["Javascript", "ReactJS", "React Native"];

// Declaracao de funcao
function temHabilidade1(skills) {
  console.log('Tem habilidade 1:', skills.indexOf('Javascript') != -1);
}

// Arrow function ES6
temHabilidade2 = skills => console.log('Tem habilidade 2:', skills.indexOf('Javascript') != -1)

// Arrow function ES5
var temHabilidade3 = function(skills) {
  return console.log('Tem habilidade 3:', skills.indexOf('Javascript') != -1)
}

// High Ordem Function
const sim = 'Sim';
const nao = 'Não';

function simNao(value) {
  return value === true ? sim : nao;
}

function concatena(value, habilidade = '') {
  console.log(`Dentre as skills (${skills.join(', ')}) existe a habilidade ${habilidade}? ${simNao(value)}.`);
}

function imprime(value, habilidade = '') {
  console.log('Tem habilidade 4:', value);
}

function condicional(test, habilidade, func) {
  if (test) {
    func(true, habilidade);
  } else {
    func(false, habilidade);
  }
}

var temHabilidade4 = function(value, values, func) {
  condicional(values.indexOf(value) >= 0, value, func);
};

temHabilidade1(skills);
temHabilidade2(skills);
temHabilidade3(skills);
temHabilidade4('Javascript', skills, imprime);

// Funcao anonima - for
console.log('Tem habilidade 5:',
  (function() {
    for (const iterator of skills) {
      return iterator.indexOf('Javascript') != -1;
    }
  }())
);

//Funcao anonima
(function() {
  console.log('Tem habilidade 6:', skills.indexOf('Javascript') != -1)
}())

temHabilidade4('Javascript', skills, concatena);
temHabilidade4('Angular', skills, concatena);

Browser other questions tagged

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