1

Hi I tried to do some calculations as following.

The weight value should subtract its value from score. But when I was doing this I got "undefined" as the score variable output. What is the reason? Did I handle the variable declarations correctly?

var score = 10;

var scoresFunc = function (weight) {
    console.log(score);
    var finalScore = score - weight;
    score = score - finalScore;
    var scoreForStartingDate = $('#scoreForStartingDate').val();
    var scoreForDuration = $('#scoreForDuration').val();
    var scoreForProjects = $('#scoreForProjects').val();

    var score = score - parseInt(weight);
    var a = '';
    for (i = 1; scores >= i; i++) {
        a += "<option value='" + i + "'>" + i + "</option>";
    }
    return a;
};
9
  • where you are getting undefined? Commented Aug 19, 2015 at 5:09
  • 1
    mention the error completely instead of saying just undefined Commented Aug 19, 2015 at 5:10
  • See the console log. score variable gave undefined Commented Aug 19, 2015 at 5:14
  • @GRTZ: Did you call the funtion? Defining a function returns nothing (undefined) so it will ALWAYS print undefined after you define a function! Commented Aug 19, 2015 at 5:15
  • for( i = 1; scores >= i ... scores is not defined at this point, which is different than undefined, and causes a Reference Error. Define scores. Perhaps you meant var scores = score - parseInt(weight);. In which case you will still get weight * -1 because at that point score is always 0. It is unclear what exactly you are getting at with this question, or what the purpose of this code was. Commented Aug 19, 2015 at 5:16

4 Answers 4

5

The reason you see undefined logged for console.log(score) is because of something called variable hoistingMDN.

var score = 10;
var scoresFunc = function (weight) {
    console.log(score);
    var score = score - weight;
};

This is a condensed version of the shown function. However, as a result of hoisting, it is actually this

var score = 10;
var scoresFunc = function (weight) {
    var score;
    console.log(score);
    score = score - weight;
};

Which should hopefully clear up why you log undefined.

Sign up to request clarification or add additional context in comments.

2 Comments

No I want get value 10 into the function. thats why i put like that
@GRTZ - Use separate variable names. You are overwriting the variable in the function scope.
1

The var score you have inside your function is likely messing things up. Remember that ALL var definitions are automatically hoisted to the very top of their function scope. That means your function is the equivalent of this:

var scoresFunc = function (weight) {
    var score, scoreForStartingDate, scoreForDuration, scoreForProjects, a;
    console.log(score);
    var finalScore = score - weight;
    score = score - finalScore;
    scoreForStartingDate = $('#scoreForStartingDate').val();
    scoreForDuration = $('#scoreForDuration').val();
    scoreForProjects = $('#scoreForProjects').val();

    score = score - parseInt(weight);
    a = '';
    for (i = 1; scores >= i; i++) {
        a += "<option value='" + i + "'>" + i + "</option>";
    }
    return a;
};

So, because you have a var score inside the function, then that overrides the global name within this scope and any references to score will use the local variable.

Therefore, this line:

score = score - parseInt(weight);

will be this:

score = undefined - parseInt(weight);

Which obviously does not work.


I don't know exactly what you're trying to accomplish in this function. Perhaps you just need to remove the var in this line:

var score = score - parseInt(weight);

so that you are operating on the higher scoped score variable, not the local variable.

Comments

1

in here: var score = score - parseInt(weight); you redefine variable score, so maybe it's caused the first operator become undefined...

also watch out for the loop, you used scores instead of score

2 Comments

No I want get value 10 into the function. thats why i put like that
man it was that, and i test it, it worked fine,... just find that line, and remove var, when you redefine the same variable you casue local variable to refer to that object. so they become undefined, remove the var and every thing will be ok. write this instead: score = score - parseInt(weight);
0

use parseFloat or parseInt like

var finalScore = Number(parseFloat(score) - parseFloat(weight));

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.