What does it mean Does the solution make comparisons against strings in the following code? JS

Asked

Viewed 4,295 times

2

The statement of the financial year is: Declare the dayFew you receive a "Sunday" string. Then implement a conditional using if that compares diaDemana is equal to "Sunday", if true print a string "Today is football day!!!".

I made the following code:

var diaDeSemana = "domingo";

if ( "domingo" === diaDeSemana) {

  console.log("hoje é dia de futebol!");

}

The solution is accepted, but it appears the warning that it compares against strings! I have already researched on the internet about the different ways of comparison, but I found no result for this error.

7 answers

7


What you did is right. I see no need to compare otherwise in an exercise than somewhere having a string.

What you can do is declare the right answer also in a variable and then compare those variables. This is sometimes used when the server gives the right answer in a fetch.

For example:

const RESPOSTA_CERTA = 'domingo';

function oQueFazerHoje(diaDeSemana) {
  if (diaDeSemana === RESPOSTA_CERTA) return 'hoje é dia de futebol!';
}

console.log(oQueFazerHoje('domingo'));

At work we use a lot of the practice of declaring static/constant string variables in uppercase letters to be clear to the programmer who is using a constant. In addition to avoiding errors, it is also good practice not to write the string several times into different variables... I created up a tool to change Java and do this in code automatically.

  • Is that it? I’ll test it and I’ll be right back!

  • 1

    Sergio, you’re right! I declared the answer as variable and then compared the two variables

  • @Stéphanieverissimo ótimo!

2

I solved this problem as follows:

var diaDeSemana = "domingo";

var dia = 'domingo';

if (diaDeSemana == dia)

{
  console.log("Hoje é dia de futebol!!!");
}

without displaying the error message!

0

I am redoing the exercise and found this solution:

var diaCerto = "domingo"
var diaDeSemana = diaCerto

if(diaDeSemana == diaCerto){
  console.log("Hoje é dia de futebol!!!")
}
  • 4

    That’s not a solution, it’ll say it’s football day for anything you put in the string. It’s partly the fault of the place that gave you the dubious exercise. I may be mistaken, but it gives me the strong impression of being a source of very bad studies, I suggest caution.

  • 1

    That’s right, thank you for the advice.

  • 1

    Anyway, the answer you marked as chosen, from @Sergio, is the most suitable of all that was posted so far. Good studies.

-1

var diaDeSemana = "domingo";    
var valorComparativo = "domingo";    
if (diaDeSemana === valorComparativo) {    
  console.log("hoje é dia de futebol!");    
}

-2

var diaDeSemana = "domingo";
var dia = 'domingo';
if ( diaDeSemana === dia) {

  console.log("hoje é dia de futebol!");

}

-3

var diaDeSemana = "domingo";

if ( diaDeSemana = "domingo" ) {

  console.log("hoje é dia de futebol!");

}

Thus and the correct Sunday being assigned to the day of the week, it cannot be compared, because we know that it is already a day of the week.

-3

let diaDeSemana = "domingo"

let dia = "domingo"

if (diaDeSemana == dia)

{
  console.log("Hoje é dia de futebol!!!");
}

Browser other questions tagged

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