Function returning Undefined javascript

Asked

Viewed 64 times

1

I have been trying to return a function where it tests if the programmer has the css skill; the skills are saved inside an array, but in return always the result is Undefined. can help me?

const usuarios = [
    {
        nome: 'Carlos',
        tecnologia:['Javascript','css' ]

    },
    {
        nome: 'leo',
        tecnologia:['js','css']
    }


]



for(let i=0;i<usuarios.length;i++)
{
{
    console.log(`Usuario com sua tecnologia ${usuarios[i].nome} ${usuarios[i].tecnologia}`)
}
}
function testacss(usuarios)
{
for(let i=0; i< usuarios.length; i++)
  {

      if(usuarios.tecnologia[i] == 'css')
      {

        return true
      }
      else {

        return false
      }


  }
}


for(let i =0; i< usuarios.length; i++)
{
    const usuariostrabalhacomcss = testacss(usuarios[i])
    if(usuariostrabalhacomcss){

        console.log(`O usuario ${usuarios[i].nome} trabalha com css`)
    }else{
        console.log(usuariostrabalhacomcss)

    }
}
  • Just change the function testacss() for function testacss(usuarios) { return usuarios.tecnologia.includes('css');}

1 answer

1

The function testacss receives as parameter a user and you need to look for the value 'css' on the property tecnologia of that user received, but you loop the user directly:

for(let i=0; i< usuarios.length; i++)

Fix this part, working with the property tecnologia user received in the parameter:

for(let i=0; i< usuarios.tecnologia.length; i++)

After this correction, note that it will always be returned false, because its function returns false where the first check is not equal to 'css':

if(usuarios.tecnologia[i] == 'css')
{

    return true
}
else {

    return false
}

What can be done here, is returns true if it is equal to 'css' and returns false after the end of the loop:

for(let i=0; i< usuarios.tecnologia.length; i++) { 
    if(usuarios.tecnologia[i] == 'css') {
        return true;
    }
}

return false;

With this, your final code will be more or less as follows:

const usuarios = [
    {
        nome: 'Carlos',
        tecnologia:['Javascript','css' ]

    },
    {
        nome: 'leo',
        tecnologia:['js','css']
    }
];

for(let i=0;i<usuarios.length;i++) {
    console.log(`Usuario com sua tecnologia ${usuarios[i].nome} ${usuarios[i].tecnologia}`);
}

function testacss(usuarios) {
    for(let i=0; i< usuarios.tecnologia.length; i++) { 
        if(usuarios.tecnologia[i] == 'css') {
            return true;
        }
    }

    return false;
}

for(let i =0; i< usuarios.length; i++) {
    const usuariostrabalhacomcss = testacss(usuarios[i]);

    if(usuariostrabalhacomcss){
        console.log(`O usuario ${usuarios[i].nome} trabalha com css`)
    }else{
        console.log(usuariostrabalhacomcss)
    }
}


Finally, if you want to search for a value in a JS array, it is very practical to use for example the method includes, see an example:

const usuarios = [
    {
        nome: 'Carlos',
        tecnologia:['Javascript','css' ]

    },
    {
        nome: 'leo',
        tecnologia:['js','css']
    }
];

console.log( usuarios[0].tecnologia.includes('css') );

console.log( usuarios[1].tecnologia.includes('css') );

Documentation: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/contains

  • Daniel fantastic. It worked super well. Thanks brother

Browser other questions tagged

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