Javascript answers for Tutorials / Codecademy

I have created a list of answers for tutorials on Javascript.
These answers were available on 8/18/2013

1 Introduction to JavaScript
An introduction to JavaScript, a beginner-friendly programming language

Getting Started with Programming
Course written by Leng Lee
Time to become a coding ninja.

Getting to know you
See what you can do with programming.

01/05 What is your name?
Hi! Let’s get to know each other. What is your name?
Write your name within quotes like this: „Ryan” Then click on Save & Submit Code.

„Any name”

02/05 Discover the length
Very good!
What would the length of your name be?
To discover the length of your name write your name within quotes. Then write a period (full stop) and the word length like this:
When you’re done click Save & Submit Code
(Do this from now on every time you finish your exercise.).
In my case (my name is Leng) it would be „Leng”.length

„Any name”.length

03/05 Basic math
Great job! Now, let’s do some math. You can do math through programming!
Calculate any addition operation you like. Add any two numbers in the form 3 + 4.


04/05 Numbers and more
See what happened? You can use the command line to do basic math operations. Try playing around some more.
You can use * for multiplication and / for division if you want. Enter another valid expression to pass this lesson.


05/05 Error: does not compute!
There are some things you can’t do in the console. Computers only speak certain languages, like the one you’ve been using today: JavaScript!
If you use words that aren’t in the JavaScript language, it will get confused and give you an error.
Try to confuse the interpreter by using a word it doesn’t know, like eggplant. It will give you a ReferenceError.


Why learn programming?
Let’s get some background to programming and JavaScript.

01/06 Editor and comments
So far, everything you’ve done has been one line in the editor. But interesting programs involve many lines of code.
You can write many lines of code, press the Save & Submit Code button, and the output will show up in the results box to the right.
You may notice that the box already has some code in it, and each line starts with //. Anything after a // is called a comment and is ignored by the computer.
Comments are extremely useful for explaining tricky parts of your code or leaving yourself reminders. You can also temporarily disable lines of code by commenting them out, or adding // in front of them.
The computer will ignore the code on lines 1-2, since it is commented out.
On line 3, find the length of the word „cake” and multiply it by 9.
We can use .length after „cake” to find the length. Then we can multiply it by 9 by using *.
If you comment out your new code, it won’t run! Make sure it’s not commented out.

// This is a comment that the computer will ignore.
// It is for your eyes only!

„cake”.length * 9

02/06 What am I learning?
This is JavaScript (JS), a programming language. There are many languages, but JS has many uses and is easy to learn.
What can we use JavaScript for?
– make websites respond to user interaction
– build apps and games (e.g. blackjack)
– access information on the Internet (e.g. find out the top trending words on Twitter by topic)
– organize and present data (e.g. automate spreadsheet work; data visualization)

Press Save & Submit Code to see an example of how JavaScript can be interactive!

There’s nothing to write down. Click Save & Submit!

03/06 Interactive JavaScript
What we just saw was a fun example of how JavaScript can be interactive. Try it yourself!
confirm(„I feel awesome!”);
confirm(„I am ready to go.”);
These boxes can be used on websites to confirm things with users. You’ve probably seen them pop up when you try to delete important things or leave a website with unsaved changes.
Write your own message that you want the user to confirm.

// Also try the Q&A forum to get help
// The link is on the bottom left of the page!
confirm(„I feel awesome!”);
confirm(„I am ready to go.”);

04/06 What is programming?
Programming is like writing a list of instructions to the computer so it can do cool stuff with your information.
Programs can’t yet make your bed, but they can do math, keep track of your bank account, or send a message to a friend.
To do any of these actions, the program needs an input. You can ask for input with a prompt.
1. prompt(„What is your name?”);
2. prompt(„What is Ubuntu?”);
Use the prompt command to ask the user where they are from.
Even tiny mistakes or typos can cause errors. Make sure to check that your punctuation is right and every opened bracket or brace is closed.

prompt(„What is your name?”);
prompt(„What is Ubuntu?”);

05/06 Data Types I & II: Numbers & Strings
Data comes in various types. You have used two already!
a. numbers are quantities, just like you’re used to. You can do math with them.
b. strings are sequences of characters, like the letters a-z, spaces, and even numbers. These are all strings: „Ryan”, „4” and „What is your name?” Strings are extremely useful as labels, names, and content for your programs.

To make a number in your code, just write a number as numerals without quotes: 42, 190.12334.
To write a string, surround the string with quotes: „What is your name?”
Write a string with at least 3 words and find the length of the string. Length counts every character in the string—including spaces!
To check the length of something, we type „string”.length. Remember a string might not always be a word. You can put almost any character in between quotes to make a string.

„Write any phrase you wish”.length

06/06 Data Type III: Booleans
The third type of data is a boolean (pronounced „bool-ee-un” and named after George Boole). A boolean can have only two values, true or false.
You can use them in your code by making statements that evaluate to true or false.
For example:
1. 10 > 3 evaluates to true
2. 5 < 4 is just crazy talk, so it evaluates to false Booleans are extremely useful because later they will let us run certain parts of our code only if certain conditions are true. For example, ATMs evaluate [the amount in your bank account] > 0 and will only give you cash if the answer is true.
Write code that will evaluate true if I’m coding like a champ! has more than 10 characters.
You can just write the condition into your editor and it will be evaluated for you.
You can use „string”.length to find the number of characters in your string.
Your code should look something like this:
„I’m coding like a champ!”.length > 10

„Any sentence bigger than 10 letters”.length > 10

Make the computer think!
Use if / else statements to generate different output for different situations.

01/06 Using console.log
You may have noticed that the interpreter doesn’t print out every single thing it does. So if we want to know what it’s thinking, we sometimes have to ask it to speak to us.
console.log() will take whatever is inside the parentheses and log it to the console below your code—that’s why it’s called console.log()!
This is commonly called printing out.
Please print the following two console.log statements at the same time. Type one on line 1 and the other on line 2. Then press Save & Submit Code.
console.log(2 * 5)
Make sure to include quotes for strings, and no quotes for numbers.
Check your parentheses carefully.
Make sure you are running two console.log statements at the same time.
On line 1, type out the first statement. Then on line 2, type out the second statement. Then press run!

console.log(2 * 5)

02/06 Comparisons
We’ve learned about three data types: numbers, strings, and booleans. Let’s learn about comparison operators and how they relate to data types.
List of comparison operators:
> Greater than
< Less than <= Less than or equal to >= Greater than or equal to
=== Equal to
!== Not equal to

Try to use each of the operators above.
Choose the right comparison operator to make each of the four statements print out true.
Write two more console.log statements that both evaluate to false.
When you use the .length, the value that is returned is a number. Therefore, when you enter something like „Jenny”.length === 5, the computer is evaluating whether the number of letters in Jenny is equal to 5.

//After you run the code, you should see true 4 times, and false 2 times.
//By using `console.log` at the start of each line,
//we are able to print 6 lines of output.

console.log(„Xiao Hui”.length<122); console.log("Goody Donaldson".length>8);
console.log(„hi there”.length>10);
console.log(10<2); 10+1

03/06 Decisions, decisions
Nice work on the comparisons! Now let’s take a look at how useful they can be.
You can use comparisons plus booleans to decide whether a block of code should run. This is called an if statement or conditional statement.
Take a look at the code on the right. The computer first looks at line 1. If the condition (in this case, 100 < 2) is true, then it executes the code inside the curly braces {}. If the condition is false, it skips the code in the curly braces entirely and goes on to the next line, which is line 6. Instructions Edit line 1 so that your program will print out both statements. Hint If the condition on line 1 executes to true, it will run the code in between the curly braces.

if ( 100 > 2 )
console.log(„You are good at math!”);
console.log(„Just letting you know: your program got to line 6″);

I will come with answers later!

Leave a comment

Your email address will not be published.