Tuesday, March 12, 2019

Beginner Coding/Programming: JAVASCRIPT



Beginner Coding/Programming


Have you ever wondered how to script? What is scripting, anyways? How do you script? What are the main languages? Well, today, I will teach you a language called Javascript. let’s start off with some questions.

Questions about programming

Let’s get things straight first. What is programming? Programming is the action of writing computer programs. Or writing scripts to tell the computer what to do. It’s like the brain. The brain is kind of like the programmer that’s telling the body what to do. Programming is part of Computer Science. And, what is a language? A language is the method of human communication, written or spoken, consisting words in a structured way. Well, in this case i’m talking about programming language. It’s the same as a language, just on a computer. So, Javascript is a programming language. There are multiple different programming languages, like we have multiple different languages.

Why programming? There are multiple different uses for programming. 1. Lot’s of businesses and jobs require programming. Well, okay, I’m not a grown up so I can’t really explain why. 2. To create games, you need to use programming. Well, you are using a computer to create your own video games, so, you will need to understand program to program your own video games. 3. I think it’s fun. Programming is hard, but still fun. There are tons of more reasons, but I’ll just explain a few.

What other names are there for “programming”? There are “scripting” and “coding”

Okay, these should answer the main questions. Now, I have a warning for you guys.

Warning:

Coding may feel totally different to you. Even if you’ve had experience with coding, there are hard and easy things you can learn. If you want to learn coding, you gotta have that positive mindset. Think about this: when you first learned a language, was it hard? That’s the same for coding--it’s like learning a totally different language. Don’t expect it to be easy. Now, the first time you learn how to code, it will be confusing. Once you reread it a few more times, you’ll understand. Have fun coding!

JavaScript

To start, I always like to learn how to type your own name in JavaScript. To type your own name, type your name between 2 quotation marks and add a semicolon at the end. For example

“Wayne”;

Nice job! In JavaScript, when we surround a word with quotation marks, that’s called a string. Strings are used to represent sequences of letters, numbers, and symbols. It’s like a variable, which is used to store information. In fact, a string is a variable, known as string variable. Let’s move on.

JavaScript also have built-in features, called functions. To call a function, simply write its name, (without quotations) and end it with a set of parentheses. Try calling the alert function:

alert();

Well, many functions like the alert function can take instructions, called parameters. By sending a string into the alert function, we can put text on the pop up box. Try putting your name.

alert(“Wayne”);

Often when programming we need to store values into a container, or box, so we can use them later. To store values, we need to use a variable. ‘var’ is short for variable. So, let’s store your name into a variable.

var firstName = “Wayne”;

Great! Now we have a variable named firstName that has a string stored inside it. Output it to see what it looks like.

firstName;

In some softwares it prints the information stored in the variable below the variable. In some other softwares it’s not like that.

So far you’ve worked out only one value type (strings), but JavaScript has many! Another example is a number. Pass any number into the alert function. Just like a function, there are no quotations surrounding the number.

alert(123);

You might have not thought of this, but JavaScript can also do math! Type out a math equation like you would write one. Or combine 2 numbers. The answer should show in the output

123 + 321

Great! JavaScript can not only combine numbers but can also combine strings. Create an alert function that combines your name and a string.

alert(firstName + “ is awesome!”);

Note: in order for the message to work, you must add a space after the first quotation.

Great! Now you have a basic idea of JavaScript. Let’s move on to more complex strings.

Strings

Remember, strings are values made up of text, numbers, and symbols. Strings are contained within a pair of quotation marks or single quotation marks and end with a semicolon. Example:

‘This is a string.’;

“This is a string, lol don’t trust that one JK”;

Now, let’s say you want to use quotation marks inside a string. What do you do now? Well, simply use the opposite quotations. So, if you’re using double quotations for the string, then use single quotations for the word(s) inside the string. If you’re using single quotation marks for the string, then use double quotation mark for the word(s) inside the string. Here is an example below:

‘Remember to say “Please” and “Thank You!”’;

Strings have their own built in features and variables, known as properties. These are some of the properties below.


Length

A string’s length property keeps track of how many characters the word has. To use it, type the words between quotations, then add a .length and end with a semicolon. The length in .length is not capitalized. Here’s an example below:

“Hippopotamus”.length;

Output

12

2. toLowerCase

A string’s toLowerCase returns all letters in the string to lowercase. To use it, type the words between quotations, then add a .toLowerCase, parentheses and close parentheses, then a semicolon. Here’s an example below:

“CHILL”.toLowerCase();

Output

“Chill”

3. toUpperCase

A string’s toUpperCase returns all letters in the string to uppercase. To use, type the words between quotations, then add a .toUpperCase, parentheses and close parentheses, then a semicolon. Here’s an example below:

“why am i so tiny?”.toUpperCase();

Output

“WHY AM I SO TINY?”

Numbers and Math

Numbers are mathematical operations. You don’t need any special syntax or symbols for numbers. Just write them straight forward ending with a semicolon. Hey, you could cheat on your homework using these. Example:

12345;

JavaScript doesn’t confuse adding numbers and decimals together. Example:

8 + 2.16347;

Output

10.16347

Sadly, fractions don’t exist in JavaScript. But you can rewrite them as decimals or division problems, like below:

1÷3




Output

0.3333333333333333

Improper fractions are the same:

12/10;

Output

1.2

To use mixed numbers, add the whole number and the fraction.

2 + (3/2);

Output

3.5

You can make negative numbers by placing the (-) in front of the operator or subtracting a number from a smaller number.

-7;




4-5;

Output

-1

Variables

Variables are values that can store any type of JavaScript value. var is the keyword for JavaScript when you declare a variable. Example:

var x = 2;

You can even use variables when declaring other variables. You can also give an existing variable a new value. Examples below:

var = 100;

x + 200;

Output

300




var x = 100;

var y = x + 200;

y;

Output

300




var day = “monday”;

day = “tuesday”;

day;

Output

“tuesday”

Functions

Functions are blocks of code that can be reused and renamed. Here’s how to declare a function.

function addTwoNumbers(x, y) {

return x + y;

}

Note that functions don’t have Parameters.

function greetThePlanet() {

return “Hello World”;

}




greetThePlanet();

Output

“Hello World”

If a function does have parameters, you’ll need to provide values inside the parentheses when using the function. Example:

function square(number) {

return number * number;

}




square(12);

Output

144



These are all the basics you should learn. Remember, if you don’t get it, it’s fine. If you reread close enough, you’ll eventually figure out. How do you think you could create your own game, or whatever? You have to know how to code. But first, the basics, right? BYE!;

1 comment:

  1. This blog post may be the longest yet. Sorry when I pasted my blog it resetted some features.

    ReplyDelete

100WC#7: The illogical definition of "I was so cross that"

I was so cross that the illogical definition of "I was so cross that" was "mad I was that" instead of "I was so ma...