# Solved: Learning JavaScript coding, whats wrong with this code?



## turbow5 (Jan 2, 2012)

If this isn't the right section forum please let me know but I stumbled across this website and I cant tell what I'm doing wrong with the coding. In attachment.


----------



## ckphilli (Apr 29, 2006)

For an if statement, you're basically saying...if x happens, do y, else do z. Here's our wiki entry on it: http://library.techguy.org/wiki/IF_Statement

For your code, you coded if x happens, do y else z do. See the difference?

Your console.log should be between the curly braces.


----------



## turbow5 (Jan 2, 2012)

I'm still not getting this, I put them between curly braces and it didn't pass me. Could you right out what its suppose to be on line 5?

That way i can compare them.


----------



## ckphilli (Apr 29, 2006)

So this didn't work?


```
else
{
console.log("did you pass");
}
```


----------



## ckphilli (Apr 29, 2006)

Might also be the codeacademy editor...as in, you may need a semicolon after else...


```
else;
{
console.log("stuff");
}
```


----------



## turbow5 (Jan 2, 2012)

It still says line 5 i s wrong, I line 6 to like 5 with and without curly braces and the ";" symbol and It says there's something wrong with the "else" and I used ";" on that also.


----------



## ckphilli (Apr 29, 2006)

Do they have the function already added for you in code academy?

This works in the real world called by a button:



> function testfunction()
> {
> if (4>6)
> {
> ...


----------



## Ent (Apr 11, 2009)

No no no no no. 
You don't put the semicolons in after the If or Else statements.
Remember that the semicolon is to Javascript what the full stop is to us.
I wouldn't say

If. you're feeling hungry have a sandwich. 

I'd say

If you're feeling hungry have a sandwich. 


The "missing operator" error is because it sees "If. " and wonders "If what?"


----------



## ckphilli (Apr 29, 2006)

Ent said:


> No no no no no.
> You don't put the semicolons in after the If or Else statements.
> Remember that the semicolon is to Javascript what the full stop is to us.
> I wouldn't say
> ...


Ent, that's why I asked about codeacademy. Their setup is different there if I remember correctly. As in, different than a normal editor.


----------



## Ent (Apr 11, 2009)

ckphilli said:


> Ent, that's why I asked about codeacademy. Their setup is different there if I remember correctly. As in, different than a normal editor.


Why ever would they do that? Very strange.


----------



## ckphilli (Apr 29, 2006)

Ent said:


> Why ever would they do that? Very strange.


It is...but again, that's if I remember correctly. It's been a while.


----------



## ckphilli (Apr 29, 2006)

I was in fact mistaken...or they changed it. Anyway, turbow5, take out the semicolons after the if and the else...so something like this:


```
if(4>6)
{
//do stuff
}
else
{
//do other stuff
}
```


----------



## turbow5 (Jan 2, 2012)

Wait I thought anything begging with a // was a note to yourself that the computer ignores? Yet it worked but now there's a error on line 9 and I don't know what I even did. (checked the Q&A and there's answers for every question except this one.) Also you said codecademy was different. What would you suggest then?

Remember to answer whats wrong with line 9 though.


----------



## ckphilli (Apr 29, 2006)

No, forget the codeacademy specific stuff.

The slashes are for comments. Remove them from your code and delete the last 2 curly braces and the comment between.

Also, your forgot the semicolons after your alert statement and console.log statement.


----------



## turbow5 (Jan 2, 2012)

What do you mean by delete the comment between. Only part you lost me on. You mean delete "fill in here:" part?


----------



## ckphilli (Apr 29, 2006)

Yes, the last 2 curly braces and the comment (//blah blah...fill in here) in between. It's unnecessary.


----------



## turbow5 (Jan 2, 2012)

Alright Finally it passed me. Any other tips I'm probably going to get stuck on?


----------



## ckphilli (Apr 29, 2006)

I would pay very close attention to curly braces and semicolons (as you've learned already). They can blow up a script in a second and can be a pain.

Also, I would recommend reinforcing your studies by running scripts outside of codeacademy. They do a lot of it for you, and you could benefit from developing a simple html page on your local machine for script testing. I'll let you google that, because you'll learn a lot more that way.

Have fun!


----------



## turbow5 (Jan 2, 2012)

I knew this wouldn't be easy to learn but I don't get what it means by this. Code below.

If("turbow5".length === 9);
do{"I am Wrong"};

It says I'm "missing while". What does that mean?...


----------



## ckphilli (Apr 29, 2006)

Give me another screenshot please, I need to see where you're at in the exercise.


----------



## turbow5 (Jan 2, 2012)

Says on line 5.


----------



## ckphilli (Apr 29, 2006)

Ok turbo, slow down a little bit. 

1) forget about the while, that has nothing to do with this.

2) This exercise is no different than the last we helped you with...if{} else {}

3) Remember:


```
//this is a comment used as a placeholder or for notes. if you see "//do something" you can't just un-comment and make it work
```
4) Look at semicolons and curly braces

5) If you look back in this thread, you'll find how to correct your issue.

Good luck, it's bedtime here.


----------



## turbow5 (Jan 2, 2012)

This time I cant figure out what I'm doing wrong. I've visited the Q&A, the first link you showed me and previous post. What is throwing me off?


----------



## ckphilli (Apr 29, 2006)

Turbo...semicolons...do not put a semicolon after an if or else.

I just don't think you're thinking about this correctly. Think about a game; a first person shooter. Things happen based on your decisions. You're learning the same thing in a script. For instance:


```
IF (you enter a certain room)

{
alert("You shouldn't be in this room");
}

ELSE

{
alert("You're on track");
}
```
You don't need "do".

These comments include "do" because they are notes and trying to tell you something. Like so:


```
IF(8>4)
{
//Do stuff
}
ELSE
{
//Do other stuff
}

IF(8>4)
{
alert("True");
}
ELSE
{
alert("False");
}
```
These are fundamentals you need to understand before you move forward. Codeacademy is a GREAT learning tool. But don't get caught up in "passing". If you just focus on passing, you aren't going to retain anything.


----------



## turbow5 (Jan 2, 2012)

*brain melting* Am I still missing a semicolon?


----------



## Ent (Apr 11, 2009)

I'm not great at Javascript myself, but I think you want the following:
Remove the DO. Do is a completely different command used for repeating code.

Insert a semicolon just before each of the closing braces, as in the following:
{console.log ("I am right");}


----------



## ckphilli (Apr 29, 2006)

Ent said:


> I'm not great at Javascript myself, but I think you want the following:
> Remove the DO. Do is a completely different command used for repeating code.
> 
> Insert a semicolon just before each of the closing braces, as in the following:
> {console.log ("I am right");}


Yes, thanks Ent. Turbo, read post #24 again. I gave you some perfect examples and said "don't use do".


----------



## turbow5 (Jan 2, 2012)

Ok got the "do" part but the error is on line 6. It says I did not log the console at all. Isn't that the console.log part?


----------



## Ent (Apr 11, 2009)

= sets a variable.
== checks whether two variables match.

What's === ?


----------



## ckphilli (Apr 29, 2006)

Ent said:


> = sets a variable.
> == checks whether two variables match.
> 
> What's === ?


That's the strict equal operator. Both type and value have to be equal to return true.


----------



## Ent (Apr 11, 2009)

ckphilli said:


> That's the strict equal operator. Both type and value have to be equal to return true.


Thanks.
I didn't know that.


----------



## ckphilli (Apr 29, 2006)

Ent said:


> Thanks.
> I didn't know that.


I think his problem is probably with semicolons again. I'm waiting to see if he rereads the examples and solves it.


----------



## turbow5 (Jan 2, 2012)

"..." If it's the semicolons again my brain will explode. I have put that in every single place to see if it was and it was still wrong.


----------



## ckphilli (Apr 29, 2006)

But you don't put them in every place. You put them after your "console.log("stuff")", only.

Post a screen shot again and I'll help.


----------



## turbow5 (Jan 2, 2012)

No, (You read that wrong) I did 1 colon between every character one at a time. starting from the end. and worked my way up. get what I'm saying? And I've put the colon at the end to. Didn't work either.


----------



## ckphilli (Apr 29, 2006)

bring {console.log("I am wrong.");} down a line.

like this


```
else
{console.log("I am wrong");}
```
It shouldn't matter, but try it anyway.


----------



## ckphilli (Apr 29, 2006)

Wait a minute...the directions say "print". So if it doesn't work, replace "console.log" with "print"


----------



## ckphilli (Apr 29, 2006)

Yup, that was it. Just did it in codeacademy to test:


```
if("ckphilli".length === 4)
{
    print("I am right");
}
else
{
    print("I am Wrong");
}
```
Note the way the code is written above. It makes it more readable when you spread it out.


----------



## turbow5 (Jan 2, 2012)

So like this? because neither of those worked.


----------



## ckphilli (Apr 29, 2006)

Change your 7 to a 8 and replace the other console.log with print.

The exercise is to return false.


----------



## turbow5 (Jan 2, 2012)

Didn't work. Beginning to think this is bugged.


----------



## ckphilli (Apr 29, 2006)

Copy and paste the code in post #38. That should tell you if it's bugged.


----------



## ckphilli (Apr 29, 2006)

Ah...you had a capital I in if. That's what blew it up.


----------



## turbow5 (Jan 2, 2012)

PICKY PICKY!
Can't believe that is what was wrong.


----------



## ckphilli (Apr 29, 2006)

It is what it is: Syntax 

Now, on the next exercise (I already know what it is, just did it), take your time and try your absolute best to solve it with what's in this thread and what you've learned. 

I'm not saying that because I don't want to help...I'm saying that because debugging is soooooooooooo important.

Have a great night!


----------



## turbow5 (Jan 2, 2012)

They should have explained this differently, can you? I sort of get it but I guess it didn't click because I'm stuck.


----------



## ckphilli (Apr 29, 2006)

Read the directions closely. They asked you to create a variable called "myAge". You created a variable called "age"....


----------



## turbow5 (Jan 2, 2012)

This is probably really simple but I just don't see it,and am i doing this right so far? Also don't mean to keep bothering you


----------



## ckphilli (Apr 29, 2006)

turbow5 said:


> This is probably really simple but I just don't see it,and am i doing this right so far? Also don't mean to keep bothering you


I'll have a look later today. And don't worry about bothering me...reinforcement is never a bad thing (as in...I don't deal with javascript everyday, so this keeps me tuned).


----------



## ckphilli (Apr 29, 2006)

You're not too far off.

Think about it this way...

integer:

```
var numbers = 12;
```
string:

```
var text = "Turbo";
```
In this exercise, they want you to give assign a string value.

So once you do that, you have no need to us "whatever".length, because it's assigned. Using the example above it would be

```
console.log(text.length);
```
and so on...


----------



## Ent (Apr 11, 2009)

And "string" means "text" in programmer speak. 
That confused me for ages when I was first learning to write software.


----------



## ckphilli (Apr 29, 2006)

Ent said:


> And "string" means "text" in programmer speak.
> That confused me for ages when I was first learning to write software.


Took me a while to get that through my thick skull as well.


----------



## turbow5 (Jan 2, 2012)

Ent, now that you say that it makes a lot more sence now. Also if i put var mycountry=14; for the next few lines do i put 14 or mycountry to recall the var?


----------



## Ent (Apr 11, 2009)

Don't say "mycountry = 14". Set it to "Japan", "England" "Brazil", or any other text string. Unless, of course, you live in 14. 

You use the name of the variable to refer to it. In this case you'd refer to "mycountry".


----------



## turbow5 (Jan 2, 2012)

So verify this for me please. If I set a variable such as: var computers=money. Every time I want to use the word computers i just have the type in money? If so then what do I put if I want to make a variable for money then? Do I have to make another variable?

Also it says I didn't .length mycountry. Course it can't hurt to see if I did it right.


----------



## Ent (Apr 11, 2009)

The purpose of a variable is to hold information which could be different every time you run the program, or could even change during one running of the program. 

Let me use a real life example, in which a first aider goes to a training session.
The trainer says "If the person is conscious, ask them their name. Keep talking to them in a calm voice, and keep using their name."
Here "their name" acts as a variable. If I were the one in shock, the first aider would keep addressing me as "Josiah". If the person were called "Dave", they would address them as "Dave". By saying "use their name" the trainer's instructions work whoever needs first aid.

In your exercise, every time the computer sees "mycountry" it looks for the value of "mycountry", which in this case is "Japan". If I were the user and had set "mycountry" to "England", it would instead use "England" every time. This makes the script flexible enough to deal with many different situations.


----------



## ckphilli (Apr 29, 2006)

Also, when assigning string variables you must enclose in quotes.

So, var whatever = "blah"; not var whatever = blah.


----------



## turbow5 (Jan 2, 2012)

Missing a operand on line 7. Am I doing this right?


----------



## Ent (Apr 11, 2009)

Let me point something out about variables. You will progress to the stage where the value of your variable is not included in your code and at which you, as a programmer, do not know the value. For example, you'll be asking the user what their name is, and then using the name that they provide. Therefore when you refer to a variable, you do not need to know what value is in it. Consider that with regards line 4.
(Line 2 is fine because you're still assigning the variable manually.)

In terms of the actual exercise, read again what you have to do.
You first need the value of _myName_ to be "turbow5"
Then you need to output the value of _myName_ to the console.
Next, you have to change the value of _myName_ to the first two letters ("tu")
Finally, you need to output the new value of _myName_ to the console.


----------



## turbow5 (Jan 2, 2012)

This is why I hated algebra. So since I don't know the variable, do I just leave it blank?


----------



## Ent (Apr 11, 2009)

Not quite. You just say to log the variable. There is no need to have *=""* after it, and that will actually lead you to some quite major bugs later on.

Try not to think of these variables as being the same sorts of variables as you get in algebra. In algebra the value of the variable doesn't change, and you're often trying to find out that value. Here, the value could change, but you're just using whatever value is in it at the moment to solve something else.


----------



## turbow5 (Jan 2, 2012)

So if I leave it blank, if I read your reply right. How do I end line 4?


----------



## Ent (Apr 11, 2009)

Line 2 was correct before. Put that back.
Line 4, you're very close. Consider how you did it right at the start, when you were logging text like "did you pass?" But instead of "_did you pass" _(including the quotes) you want _myName_ (with no quotes). The computer will then look at the variable called _myName_ and see what its value is. That value is what will go to the log.


----------



## turbow5 (Jan 2, 2012)

I know what is going to kill me doing this now. Is this how you finish line 7? I thought you had to have another ) after the last one to end it.


----------



## ckphilli (Apr 29, 2006)

So close! You set myName="Turbo" on line 2....so on line 4 you have no reason for the "=turbo".


----------



## turbow5 (Jan 2, 2012)

This forum is going to turn out to be the longest forum on this website. Anyways did you mean get rid of the "turbow5" on line 4?....


----------



## ckphilli (Apr 29, 2006)

Yes, also take out the ="". You already set myName = to turbo, so on line 4 you're just writing what you set it to.

Leave lines 2 and 9 alone.

Line 7 hint:back up one exercise and look at substring.


----------



## turbow5 (Jan 2, 2012)

What's wrong with the substring?


----------



## Ent (Apr 11, 2009)

There are two things wrong with it.
Firstly, _myName_ is green.  And if you look at the rest of the code, you'll realize what makes it green.

Secondly, while that (once you fix the first problem) will return the first two letters of _myName_, you still need to store them back into the original variable.


----------



## turbow5 (Jan 2, 2012)

Yeah I got the green part, But i don't see what's wrong with it still.

(myName.substring(0,2));


----------



## Ent (Apr 11, 2009)

You're almost there. Remember, the task is to *change *the value which is stored in _myName_.

_(myName.substring(0,2));_ translates to the first two letters, but it doesn't say what to do with them. Is it supposed to print them out, say them aloud, make them do a little dance? What you want to do is set the value of _myName_ to those two letters.


----------



## turbow5 (Jan 2, 2012)

This right? It passed me but it doesn't look right.


----------



## Ent (Apr 11, 2009)

No, it's not quite right.
You have to change the value of _myName_ on line 7, not just print it out.
You should get "turbow5" printed out from line 4,
And "tu" printed out from line 9.


----------



## turbow5 (Jan 2, 2012)

Can you give me a tip on how to change the value then? I thought that was the point of setting the var of myName.


----------



## Ent (Apr 11, 2009)

That is the point of the line 
var myName="turbow5";

You have to change it *again*, to "tu" on line 7.


----------



## turbow5 (Jan 2, 2012)

Ok got all that done. Think I'm loosing it with this stuff after each lesson. This is how to log the length of the word right?


----------



## Ent (Apr 11, 2009)

You're close. The problem is with line 3. Again, think about the colours. You need to set the value of myJob to the string "Computers".

I should say though, while they may seem useless because the computer doesn't read them, it is very much worth your while using comments that are as descriptive as they can be. They don't cost anything except a moment of time, and it will help you when you or someone else has to come back and read your code some time in the future.


----------



## turbow5 (Jan 2, 2012)

If/then statements again. I don't see what's wrong with the last line.


----------



## Ent (Apr 11, 2009)

Funny how people get all this fancy stuff sorted out, but then forget basic first grade arithmetic. 
What is 2 + 2 ?


----------



## turbow5 (Jan 2, 2012)

I wanted a false to be first this time. Does it always have to be true first then false?


----------



## Ent (Apr 11, 2009)

How do you expect the computer to know that you wanted it to do the first thing if it were false?
Yes, you have to write the true section first. 

If someone said "If you have a ticket stand in line A, otherwise stand in line B", that would clearly mean that people who do have tickets take the first option, and people without tickets take the second option. It's exactly the same in this script.


----------



## turbow5 (Jan 2, 2012)

I did not log the correct string to the console... This one stopped me in my tracks. I thought you suppsoe to log them both?


----------



## turbow5 (Jan 2, 2012)

Failed to upload image.


----------



## Ent (Apr 11, 2009)

I can't see what's wrong there. Sorry.


----------



## ckphilli (Apr 29, 2006)

Don't know Turbo. I just tried it in codeacademy with no problems. Here's what I did:


```
if(2+2 > 4)
{
    console.log("test");
}
else
{
    console.log("I finished my first course!");
}
```
Maybe you have an extra space somewhere? I tried to repeat your error and can't. Regardless, I wouldn't sweat over this...move forward.


----------



## turbow5 (Jan 2, 2012)

I couldn't get it either, I had to copy and paste yours. Am I done with this? there's no next section. If I am done can I start making programs/mods/whatever else?


----------



## ckphilli (Apr 29, 2006)

No, that was just the intro. There are several other JavaScript tutorials on that site.


----------



## turbow5 (Jan 2, 2012)

Yeah after you replied I got a email for a new course.


----------



## turbow5 (Jan 2, 2012)

I'm multitasking to much to look this up at the moment, sorry but when do threads usually close down from no responses?


----------



## turbow5 (Jan 2, 2012)

Never mind I found it. But for the looping part of JavaScript is this how you loop?


----------



## Ent (Apr 11, 2009)

I suggest that you do a bit more research. _for_ is a command used for loops, but you still need quite a lot.
For example, you need some way of telling the loop when to stop. Go to google and ask it about javascript For.

Also, the actual code that you want to repeat should go inside curly brackets {} after the actual _for_ statement, just like the code you used with _if_.


----------



## ckphilli (Apr 29, 2006)

I agree wholeheartedly with Ent. Additionally, you need to actually read what they have right there in codeacademy. Something you need to understand about programming is that it's not just regurgitating code...there's some actual thought behind it. I've said this before to you and I'll say it again: slow down and actually try to learn. Else, you will constantly be backtracking because you never laid a foundation. Good luck and don't forget to have fun.


----------

