Comparison of strings in exercise!

Asked

Viewed 638 times

-3

My exercise reads as follows::
Define the philosophic functionHipster you receive as a parameter: a person’s profession (string), nationality (string) and the number of miles he walks per day (number). With these parameters assess whether or not this person is (true / false), a philosopher Hipster. Keep in mind that a philosopher Hipster is a Musician, born in Brazil and who likes to walk more than 2 kilometers a day.

I used as code the following:

function filosofoHipster(profissao, nacio, km){  
  return profissao == "Músico" && nacio == "Brasil" && km >= 2;  
}

However, the site returns that, although the solution worked, one of the objectives was not met, and the reason would be the comparison against strings!
Is there any other way to solve this?
Thanks!

  • Maybe because your comparison is casesensitive? Also has the issue of accents (ignore or not)

  • In case the site asks for the accent and the capital, if I take it gives that the exercise has not been solved!

3 answers

2

If you put 'musico' and 'brazil' in a var, it will solve your problem.

function filosofoHipster (profissao,nacionalidade,km){
var prof = 'Músico'
var nac = 'Brasil'
  return (profissao == prof && nacionalidade == nac) && (km > 2)

}

filosofoHipster('Músico','Brasil',3)

1

You can also do so:

function filosofoHipster(profissao, nacionalidade, km){
 return (profissao === 'Músico' && nacionalidade === 'Brasil') && (km > 2)
} 

filosofoHipster('Músico', 'Brasil', 3) // true

0


I’ve seen that same question several times on the site, and none of them offered enough context for a good answer.

I believe that this question wants you to declare constants in your code to make it more scalable, but in the posted statements have none of that.

What I would suggest is to try something like:

const profissoes = {
  MUSICO: 'Músico',
  // outras profissões podem eventualmente serem inseridas aqui
};

const nacionalidades = {
  BRASIL: 'Brasil',
  // outras nacionalidades podem eventualmente serem inseridas aqui
}

function filosofoHipster(profissao, nacio, km){  
  return profissao == profissoes.MUSICO && nacio == nacionalidades.BRASIL && km >= 2;  
}

That way, if you use these same constants throughout your code, and for some reason need to change the value of these constants eventually, you could do it by just changing that chunk of code.

  • The worst is that the statement is just that, in previous exercises we have not learned about constants yet, so I have not used! Plus, their answer was exactly what they wanted! Thank you so much.

  • could not be compared only with == instead of === ?

Browser other questions tagged

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