What is JAVA?
Java is one of the most popular programming languages used to create Web applications and platforms. It was designed for flexibility, allowing developers to write code that would run on any machine, regardless of architecture or platform. Also, many games are used and made with JAVA, for example; Minecraft and UNITY Software.
Lessons done for JAVA on CodeAcademy:
Unit 1:
Lesson 1:
SYNTAX Learnt:
myString:
Any grouping of words or numbers surrounded by single quotes:
' ... ' or double quotes " ... ".myNumber:
Any number, including numbers with decimals, without quotes:
4,8,15,16,23,42.myBoolean:
This is always one of two words. Either or
true,false with no quotations.Lesson 2:
I learnt how to ask JavaScript to talk to us.
To do this, you need two things:
- A way to ask JavaScript to talk.
- Something for JavaScript to say.
We can ask JavaScript to print words to the console with this line of code:
console.log('Your message here.');
In human-speak, this is saying: "Hey console, please print/log this thing inside the parentheses. Bye, thanks!"
By writing this line, we've also solved the second thing we need: Something for JavaScript to say. We can put a String, Number, or Boolean (or any data type) inside the parentheses of a
console.logstatement.
Lesson 3:
Don't worry, math does not need to be your strong-suit to learn JavaScript. There are just a few operations we'll need to know to make some awesome programs later in the course!).
JavaScript includes the general math operators that you can find on a calculator:
- Add:
+ - Subtract:
- - Multiply:
* - Divide:
/
These all work how you might guess:
3 + 4 will equal 7, 50 / 10 will equal 5.
Let's use each one of these math operators.
Lesson 4:
Now let's generate a space fact while we learn a brand new operator called – drum roll please – the modulus.
The idea behind the modulus is to show you the remainder after you divide a number.
So, if you divide
13 / 5, 5 goes into 13 two times, and there will be 3 remaining. A modulus, denoted by a %, would take 13 % 5 and return the remainder 3.
How on Earth is this useful?
Let's ask a question a modulus can solve: What will the moon phase be one year from today?
Lesson 5:
As it turns out, JavaScript has some tricks up its sleeve to make our lives easier.
JavaScript has built in functions, which help us do everyday things. We'll learn more about functions later in the course, so don't worry about understanding what they are right now.
Sometimes it's necessary to generate a random number within a program. We can do that with this code:
Math.random();
This code will return a random number between 0 and 1. JavaScript will generate a random number for us with this code.
To generate a random number between 0 and 50, we could multiply this result by 50, like so:
Math.random() * 50;
The problem with this is that the answer will most likely be a decimal. Luckily, JavaScript has our back with another built in function called
Math.floor. Math.floor will take a decimal number, and round down to the nearest whole number. It is used like this:
Math.floor(Math.random() * 50);
In this case:
Math.randomwill generate a random number between 0 and 1.- We then multiplied that number by
50, so now we have a number between 0 and 50. - Then,
Math.floorwill round the number down to the nearest whole number.
Let's utilize these two methods to generate a random number between 0 and 100.
Lesson 6:
As we write JavaScript, we can create comments in our code.
Comments are lines that are not evaluated when the code runs. They exist just for human readers, in other words. Comments can be extremely useful when we're looking back at code we've written later on and for other people who will be looking at your code.
There are two types of code comments in JavaScript:
- A single line comment will comment out a single line, and is denoted with two forward slashes
//preceding a line of JavaScript code.// The first 5 decimals of pi console.log('Pi is equal to ' + 3.14159); - A multi-line comment will comment out multiple lines, and is denoted with
/*to begin the comment, and*/to end the comment./* console.log('All of this code'); console.log('Is commented out'); console.log('And will not be executed); */
Conclusion to Unit 1:
Let's take one more glance at the concepts we just learned:
- There are three essential data types in JavaScript: strings, numbers, and booleans.
- We can write out anything to the console with
console.log. - We can do math with operators like
+,-,*, and/. - We can find the remainder after dividing two numbers with a modulus:
%. - We can generate a random number with
Math.random, then make it a whole number withMath.floor. - We write a single line comment with
//and a multi-line comment with/*and*/.
You've just finished one of the toughest parts of this course, nice work!
Unit 2:
Lesson 1:
Let's dive in and see a variable in the wild. Here is how you declare a variable:
var myName = 'Arya';
console.log(myName);
// Output: Arya
You can almost read it aloud: "Variable myName is equal to Arya."
Let's dissect that statement and look at its parts:
var, short for variable, is the JavaScript keyword that will create a new variable for us.myNameis chosen by a developer (that's you!). Notice that the word has no spaces, and each new word is capitalized. This is a common convention in JavaScript, and is called camelCase.=means to assign whatever's next to the variable.'Arya'is the value that the equals=assigns into the variablemyName.
After the variable is declared, we can print the variable with:
console.log(myName). This will print 'Arya' to the console.Lesson 2
If variables can hold strings, can they hold other data types? Let's give it a shot:
var myAge = 15;
var likesChocolate = true;
console.log(myAge);
// Output: 15
console.log(likesChocolate);
// Output: true
Variables can hold any data type, like strings, numbers, and Booleans. They can also hold data types that we have not learned yet, like arrays, functions and objects (more on that later).
Why do we care about variables?
Variables are useful in two ways:
- They allow us to use the same value over and over, without having to write a string or other data type over and over.
- More importantly, we can assign variables different values that can be read and changed by the program without altering our code.
For example, a weather app can show you a different high temperature every day. Instead of writing a new website everyday, they store the information in a variable and just change the value of that variable.
We can change a variable's value if we want, like this:
var weatherCondition = 'Monday: Raining cats and dogs';
weatherCondition = 'Tuesday: Sunny';
console.log(weatherCondition);
// Output: 'Tuesday: Sunny'
- We created a variable by using the keyword
varand the nameweatherCondition. - Then, we took the existing
weatherConditionvariable, and set its value equal to'Tuesday: Sunny'.
True to their name, variables are indeed variable.
In the previous lessons, we've put strings into variables. Now, let's put a variable's value into a string!
Putting a variable in a string uses concepts we've already learned. The JavaScript term for this idea is interpolation.
Interpolwhat?! —Possibly the most fun JavaScript term to say.
We can use the
+ operator from earlier to interpolate (insert) a variable into a string, like this:
var myPet = 'armadillo';
console.log('I own a pet ' + myPet + '.');
// Output: 'I own a pet armadillo.'
Conclusion to Unit 2
You made it to the end of the first unit and this lesson! High five!
We learned:
- How to create variables.
- How to change a variable's value.
- How to interpolate, or insert, a variable into a string.
In the next lesson, we will learn how to program JavaScript to make decisions for us and how to generate random numbers.
No comments:
Post a Comment