Why doesn’t my program display the output values?

Asked

Viewed 118 times

4

var anoAtual = 0
var anonasci = 0

const idade = function(cl){
    anoAtual - anonasci
    console.log(anoAtual - anonasci)
}

const imprimirresultado = function(c){

    if(idade >18){
        console.log(` sua idade é ${idade} você já pode dirigir`)

    }else if(idade < 18){
        console.log(`sua idade é ${idade} você ainda não pode dirigir`)
    }
}
anoAtual= 2020
anonasci= 2005

imprimirresultado()

When running on console I get no return.

3 answers

10


There are several problems with the code. Some don’t stop it from working, but that’s not how you make real code and you might be learning wrong if you keep doing it this way, so I’m going to show you the right way.

If you’re going to use functions, use them the way they were designed. You pass data to it, then it executes something, and eventually results in something that is returned. This does not exist in your code, it takes data from outside of them and this although it works is wrong, it will cause problem in the future when executing more complex codes

So let’s put parameters and return a data in the function where it should happen. All communication of the function with the external world to it should be given in this way do not take data that does not belong to it, unless it is an object that it belongs to, there you can take the this, but do not do with global data (there is even exception to this, but only after you master how you do everything else deal with these cases, although you can always do without anything global, only is not ideal in all cases).

Making the first function return something then we can call it and have the age calculated.

Did you notice that I changed function names? Give meaningful names for everything, for variables and mainly for functions. At first it may seem silly, but it helps to make the code correctly and understands what it should do.

There, within the second function I call the first calculating age (in a simplified way, obviously this has error and several cases, does not validate anything, but for a coding exercise this is not so important, only to be clear that the intention is not to calculate the age in the correct way.

With the die returning by this function I compare it to the age allowed to drive. Note that the comparison was wrong, it may not be important, but I think this may be an encoding error and not just a requirement, so I’m talking. In your comparison only 19-year-olds can drive, and I doubt that was the intention. To accept that people 18 years old can drive would have to use the largest operator or equal. I could also compare against 17, which then the bigger operator works because it accepts 18.

I took the else if because he has no sense, only the else It solves because either the person is 18 years or older, or she doesn’t have it, there’s no point in comparing something that is already known. If the first condition fails it must fall in the second every time, becomes simpler and more semantic.

One of the mistakes I was making was trying to call the function as if it were a variable. To call functions you should always do it with parentheses, and passing the arguments she expects to receive. In another language it would make an error there because the variable doesn’t even exist. Some people think that learning Javascript is easy, but hiding an obvious mistake like this for me generates confusion and misfortune, I think much worse, it may be easier for those who have no logical thought, but then one should not program.

Or it could be that I wanted to create the variable, but then forget to call the function and save its value in the variable. It is even difficult to interpret what the intention was, but it does not matter because both would be wrong in this case.

I used a normal function than a variable that receives a function. This case does not need the anonymous function which is a resource that has cost and does not serve for anything. Do not use something without need. When you need it then learn to use it this way and only for cases where it is useful.

I put ; because it works without, but there is case that gives problem, it is rare, but gives, and the day that finds with you can be suffering a lot because of it. Get used to making the code as organized as possible, it helps to think better and understand well what you are doing. The arm does not fall to type a few extra characters in the code.

And finally I put together all the code that was global. Not to mention that some variables need to be "declared" before using within the functions. This builds a misconception of the functioning of the functions.

I used the let, but know that it only works in relatively modern browsers, only use it if you will use a utility that lowers code for older browsers or if it has guarantees that it will only run on browser modern, which is the case for an exercise. Yes, developing software is much more complicated than just writing command in the language.

function calculaIdade(anoAtual, anoNascimento) {
    return anoAtual - anoNascimento;
}

function imprimeSePodeDirigir(anoAtual, anoNascimento) {
let idade = calculaIdade(anoAtual, anoNascimento);
    if (idade >= 18) {
        console.log(` sua idade é ${idade} você já pode dirigir`);
    } else {
        console.log(`sua idade é ${idade} você ainda não pode dirigir`);
    }
}
let anoAtual = 2020;
let anoNascimento = 2005;
imprimeSePodeDirigir(anoAtual, anoNascimento);

I put in the Github for future reference.

Changing a detail in your code may make it work, but it will still be considered wrong. I hope I have helped to learn the correct form and can evolve in the next code.

Fiat 147 todo detonado andando pelas ruas

7

There are some points that are preventing your code from displaying something on the console.

  • You use the variable idade in the conditions within the imprimirresultado, but this variable actually contains a function.
  • Both declared functions receive an unused parameter.
  • The function present in idade, calculates the age but does not return the same, just displays in the console.
  • Your age conditions check higher than 18 and lower than 18, but if the age is exactly 18, nothing will be done.

Some possible corrections:

The function present in idade, passes to return the calculation of age, in addition the unused parameter is removed, it is also possible to remove this console.log, but this you decide:

const idade = function() {
    let idadeAtual = anoAtual - anonasci;
    console.log(idadeAtual);
    return idadeAtual;
}

The function present in imprimirresultado, uses to execute the function idade and with their return, displays the information about 18 years, and the comparison of 18 years, begins to verify >= 18, the parameter was also removed since it was not being used:

const imprimirresultado = function(){
    let idadeAtual = idade();

    if(idadeAtual >= 18){
        console.log(` sua idade é ${idadeAtual} você já pode dirigir`);
    }else if(idadeAtual < 18){
        console.log(`sua idade é ${idadeAtual} você ainda não pode dirigir`);
    }
}

With this your code is already executed correctly and display the values in the console, below the full code:

var anoAtual = 0;
var anonasci = 0;

const idade = function(){
    let idadeAtual = anoAtual - anonasci;
    console.log(idadeAtual);
    return idadeAtual;
}

const imprimirresultado = function(){
    let idadeAtual = idade();

    if(idadeAtual >= 18){
        console.log(` sua idade é ${idadeAtual} você já pode dirigir`);
    }else if(idadeAtual < 18){
        console.log(`sua idade é ${idadeAtual} você ainda não pode dirigir`);
    }
}

anoAtual= 2020;
anonasci= 2005;

imprimirresultado();

  • pq it returns 15 before returning the result?

  • Because within the function you assigned in age, there is a console.log. I even quoted him in the explanation, he has no need, but I did not remove, I left him there for you to decide what you want to do with him =)

4

The problem with your code is that the variable idade of function imprimirresultado does not exist in the context of the function, to solve I added a return in the function that calculates age and put the value for a variable i which is used for comparison, see working below:

var anoAtual = 2020
var anonasci = 2005

const idade = function(cl){
    return(anoAtual - anonasci);
    console.log(anoAtual - anonasci)
}

const imprimirresultado = function(c){
var i = idade();

    if(i>18){
        console.log(` sua idade é ${i} você já pode dirigir`)

    }else if(i< 18){
        console.log(`sua idade é ${i} você ainda não pode dirigir`)
    }
}
anoAtual= 2020
anonasci= 2005

imprimirresultado()

  • vlw man, I was trying to declare the variable age as this

Browser other questions tagged

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