Basic JavaScript - Understanding Uninitialized Variables

Tell us what’s happening:
Describe your issue in detail here.
I have tried everything and i still unable to solve this. I don’t get it. If someone could jus help me solve it i am sure i will better understand it

Your code so far

// Only change code below this line
var a = 6;
var b = 16;
var c
// Only change code above this line

a = 5;
b = 10;
c = 'I am a String';

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/102.0.0.0 Safari/537.36

Challenge: Basic JavaScript - Understanding Uninitialized Variables

Link to the challenge:

you’re not to change the line below:
you ought to have;
var a = ?;
var b = ?;
var c = “string”; //the changes is just the top code
only change the line above, the line below remains thesame.
a = a + 1;

b = b + 5;

c = c + " String!";

Hey! Welcome to the freeCodeCamp’s community forums.

When you submit your code to freeCodeCamp, it is tested against some predefined conditions which check if your code is right or not. So this line is there for a reason. I would suggest you to reset the challenge and start again or undo to the starting point so you dont get unexpected results when running the tests.

As for this particular challenge, this is what your task is:

You’re supposed to “initialize” 3 variables which basically means you need to provide an initial value to all three variables because when you first declare (want to add a new variable in your program ) a variable with var or let, it has an initial value of undefined.

and which in turn means that when this porting of your code runs,

image

the values of those variables will be substituted to solve the expression and the result will be something like

a = undefined + 1;
b = undefined + 5;
c = undefined + " String!";

which is most likely not what we want, and that is exactly why you have the hints on the bottom of your instructions which tell you helpful things like this.


if we look at the first hint, it tells us that the final value of a should be 6 so which means we need to initialize of value of a to 5 by doing this.

var a = 5;

so when this portion of the code is evaluated, we get our desired output.

a  = a + 1
// now the final sum will be 6

Hope this helps! :smile:

2 Likes

Thank you. I think this will help

1 Like