Doubt with error - comparisons against strings

Asked

Viewed 353 times

0

Hello, Good Afternoon!

I am on a platform of studies in which exercises and tests are performed, the statement is:

Write the function can SeAposentar which receives by parameter the age, sex and years of social security contributions a person has, for example: podeSeAposentar(62, "F", 34) true.

The minimum retirement age for women is 60, while for men it is 65. In both cases, must have at least 30 years of contribution.

I made the following code:

function podeSeAposentar (idade, sexo, anosTrabalhados) {
    if(anosTrabalhados >= 40) {
        if(sexo == "F") {
            if(idade >= 60) {
                return true;
            }
        }
        else if(sexo == "M") {
            if(idade>= 65){
                return true;
            }
        }
    }
    return false;
}

And the platform reports that:

Your solution worked, but points out the following error:

Objectives that were not met: podeSeAposentar makes comparisons against strings.

Could someone help me and why does this happen? Grateful!

4 answers

0

my answer to that question is:

function podeSeAposentar(idade, sexo, anosContrib) {
    return (sexo == "M" && idade >= 65 && anosContrib >= 30) || (sexo == "F" && idade >= 60 && anosContrib >= 30)
}

0

You can use more "different" forms, for example, in the case, not using strings ("but which does not differentiate much, because the value can be’m' and 'f'"):

const MASCULINO = 0;
const FEMININO = 1;

function podeSeAposentar(idade, sexo, anosTrabalhados) {
    if (anosTrabalhados <= 30) {
        return false;
    }

    switch (sexo) {
        case FEMININO:
            return idade >= 60;
            break;

        case MASCULINO:
            return idade >= 65;
            break;
    }
}

Note: I changed the yearsWorked to 30, on account of: "...must have at least 30 years of contribution"

0

The remark is trying to tell you that you should convert numerical values to numbers before using them in comparisons.

You can use the function parseint to convert strings to integer numbers.

function podeSeAposentar (idade, sexo, anosTrabalhados) {
    // Converte valores númericos para Number
    anosTrabalhados = parseInt(anosTrabalhados, 10);
    idade = parseInt(idade, 10);

    // Agora sim realiza os teste
    if (sexo === 'M') {
        return anosTrabalhados >= 30 && idade >= 65;
    } else {
        return anosTrabalhados >= 30 && idade >= 60;
    }
}

PS: the only comparisons of your code are with the 3 parameters that your function defines, although the statement explains the example podeSeAposentar(62, "F", 34), if this observation is occurring is because the platform passes the numbers as a string, contrary to what it itself states in the statement itself.

  • Fernando, yes, and doing it this way or the other way the error makes comparisons against strings remains. What to do?

  • I think the most important thing is that you pass the link of this exercise... So it’s easier to analyze... I did what I could with what was written in the question. D

  • Fernando, this is it: https://mumuki.io/br/exercises/4511-programm_imperativa-logica-booleana-um-exercicio-sem-precedents

  • Their website has bug, I just posted my solution there and says: "the solution should declare a function podeSeAposentar". Run your code in the browser or https://codepen.io and test it yourself.

  • Yes, but as the platform of studies is there, even running ok on others in this would need to run kkk. :/

  • I just set a function podeSeAposentar, and the site ran every test on my function (which passed every test)... There’s an observation saying that I have to create a function called "podeSeAposentar". What I mean by that is what the website has bug, and maybe your question would never open if there was this bug. If your college degree or course depends on this platform, you should contact your teachers, otherwise I can only help you solve problems that really exist in your code. Their bug don’t know how to fix. :|

  • Thanks for the tips and help Fernando!

Show 2 more comments

0


The code can be simplified in this way:

function podeSeAposentar(idade, sexo, anosTrabalhados) {
    return anosTrabalhados >= 40 && idade >= (sexo === 'F' ? 60 : 65);
}

If he keeps complaining, you can replace it:

sexo === 'F'

That’s why:

sexo.charCodeAt(0) === 70
  • 1

    Finally the platform accepted, putting: sex.charCodeAt(0) == 70. Grata!

Browser other questions tagged

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