My function that returns a true/false boolean if it exists or not, is that correct?

Asked

Viewed 6,079 times

1

I am doing some exercises of js and I would like to know if my function is correct!

1) 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 Habilidade(skill){
            if(skill == 'javascript'){
                console.log('Você sabe javascript');
            } else if(skill == 'reactjs'){
                console.log('Você sabe reactjs');
            } else if(skill == 'React native'){
                console.log('Você sabe React native');
            } else{
                console.log('Você não sabe nenhuma das linguagens');
            }
        }

        var resultado = Habilidade('javascript');
        var skill = ['javascript', 'reactjs', 'React native'];

  • 1

    By the description of the problem, the function does not do what it says.

  • Your function does not receive the array in your example, and even if it did, it would not work the right way, besides, it does not return a boolean value

  • You will need to have the function go through all the values of the array passed as parameter in the function, based on this you compare if there is the value, if there is the value requested, you return true, otherwise false

  • Denis could you explain me in more detail? Thank you.

  • "My function is correct" - Could improve your question by adding part of the doubt that is in the content.

  • if you want to do one more you can use regex and check if a phrase has a certain word or expression

Show 1 more comment

6 answers

2


1) 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?

Utilize index if the return is greater than or equal to 0 exists in the array the value sought, example:

function habilidade(skill, find) {
  return (skill.indexOf(find) >= 0);
}

var skill = ['javascript', 'reactjs', 'React native'];
var resultado = habilidade(skill, 'javascript');
console.log(resultado);

or you can go through this array, example:

function habilidade(skill, find) 
{
    var i = 0;
    while(i < skill.length)
    {
        if (skill[i] == find)
        {
            return true;
        }
        i++;
    }
    return false;
}

var skill = ['javascript', 'reactjs', 'React native'];
var resultado = habilidade(skill, 'javascript');
console.log(resultado);

2

I did so:

function temHabilidade(skills) {
 skills.forEach(item => console.log(item === "Javascript"))
}

var skills = ["Javascript", "ReactJS", "React Native"];
temHabilidade(skills); // true ou false

0

Another easy way to do it:

function Habilidade(){
			
  if(skill.includes('javascript')) {
     console.log(true);
   } else {
     console.log(false);
   } 
}

var skill = ['javascript', 'reactjs', 'React native'];
var resultado = Habilidade();   

0

I managed to do it that way

function temHabilidade(skills) {
        var resultado = skills.indexOf("Javascript");

        if (resultado >= 0) {
            return true;
        } else {
            return false;
        }
    }
    var skills = ["Javascript", "ReactJS", "React Native"];
  
    console.log(temHabilidade(skills));

-1

<script> function temHabilidades(skills) { var skill; if (skills.indexOf('Javascript')) { return false; } else { return true; } } var skills = ['Javascript']; console.log(temHabilidades(skills)); </script>

-3

        function temHabilidade(skills) {

        var skill;
        if (skills.indexOf('Javascript')) {
            console.log(false);
        } else {
            console.log(true);
        }
    }
        var skills = ['Javascript'];
        temHabilidade(skills);
    </script>

Browser other questions tagged

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