Function values returning Undefined in Javascript

Asked

Viewed 224 times

-1

var const1 = function (a,b,c,d){
return  (a*b+c-a)/d; {



const1(10, 2, 2, 10, 2);
console.log('a = ' + const1.a);
console.log('b = ' + const1.b);
console.log('c = ' + const1.c);
console.log('d = ' + const1.d);

Why the values shown through the console.log are returned as undefined and not as the values that are stored in the variables.

  • Did any of the answers solve your question? Do you think you can accept one of them? Check out the [tour] how to do this, if you haven’t already. You would help the community by identifying what was the best solution for you. You can accept only one of them. But you can vote on any question or answer you find useful on the entire site (when you get enough score, it will happen as soon as you accept).

2 answers

5

They are undefined because they don’t exist. You’re trying to access an object that in theory should have these members, but what you have is an object with a unique numerical value without members. For some reason you think a function is an object and it is not, and in this case so little returns an object.

This code makes no sense. Its function returns one calculated value and can use it thus:

function calculo(a, b, c, d) {
    return (a * b + c - a) / d;
}

console.log('a = ' + calculo(10, 2, 2, 10, 2));

I put in the Github for future reference.

If you want to return 4 different operations, you would have to return an object or array with the results you want, but in this case you would have to define what these 4 operations are to put each result in a member, then you could write the code accessing the way you want, which seems a little weird, but it would be something like this:

function calculo(a, b) {
return { adicao : a + b, subtracao : a - b, multiplicacao : a * b, divisao : a / b, };
}

var resultados = calculo(10, 2, 2, 10, 2);
console.log('adicao = ' + resultados.adicao);
console.log('subtracao = ' + resultados.subtracao);
console.log('multiplicacao = ' + resultados.multiplicacao);
console.log('divisao = ' + resultados.divisao);

I put in the Github for future reference.

Finally it may be that wanted to create variables and not use literals (we have no way of knowing without the question give details):

function calculo(a, b, c, d) {
    return (a * b + c - a) / d;
}

var a = 10;
var b = 2;
var c = 2;
var d = 10;
var e = 2;
console.log('a = ' + a);
console.log('b = ' + b);
console.log('c = ' + c);
console.log('d = ' + d);
console.log('resultado = ' + calculo(a, b, c, d, e));
//note que a variável e não é usado em lugar algum, mesmo no original isso é um erro conceitual ainda que não dê erro

I put in the Github for future reference.

Actually it could be a lot of other things I wanted to do.

The fact that you don’t give meaningful names in the code helps you not understand what you’re doing and even more so that other people understand what your need is. It helps less that the question doesn’t describe what it wants, it just tells you what’s wrong. Developing software is first of all conceptualizing correctly what you are doing, the code is a consequence of this.

3

const1 does not have the definition of a, b, c or d. const1 is a function that has as return (number) the result of an equation ((a*b+c-a)/d), in the case const1(10, 2, 2, 10, 2); returns as value 1.2.

An example of how to assign const1 the definitions of a, b, c or d:

var const1 = function (a,b,c,d){
    return  {a,b,c,d, resultado: (a*b+c-a)/d}; 
}
var result = const1(10, 2, 2, 10, 2);

Thus result is of value: {a: 10, b: 2, c: 2, d: 10, resultado: 1.2}

console.log('a = ' + result.a); // a = 10
console.log('b = ' + result.b); // b = 2
console.log('c = ' + result.c); // c = 2
console.log('d = ' + result.d); // d = 10
console.log('resultado = ' + result.resultado); // resultado = 1.2

Browser other questions tagged

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