sábado, 29 de julho de 2023

001 - 3 Things The Computer Does

 

 

 

 

Since Abelardo until today, a lot has changed in the world of computers, but I'm going to talk about the 3 things that started me in this universe.

For this, I will show you JavaScript, a language that is mainly used to program web pages.

The text below is an html. It is the text that tells the browser how to assemble the page. There are things in this text that you may not understand yet. I could explain, but I'd rather tell you something else, something I've learned about learning: don't let what you don't understand bother your eyes. Seriously man, you can even read a book and understand 30 percent, and still come out on top. But this takes practice, start with small things. Forget that ghost that says you're not smart.

Then copy the text below into Notepad (yes, Notepad will do) and say: I'm not going to let this thing I didn't understand bother my eyes. If I need to, I'll learn later, but now I want to focus on what I'm learning today.

Then save the file as “umapagina.html” and then click on it through the file explorer.

 

<!doctype html>

<html>

                <head>

                               <meta charset="utf-8">

                               <script src="instructions.js"></script>

                               <title> Cool Page  </title>

                <head>

 

                <body>

                               <h1> This page is cool </h1>

                               <p>

                               Yeah I looked and it's a pretty cool page

                               </p>

                              

                              

                <body>

</html>

 

You may have noticed that the parts in bold are what is written. You can change them, and the text will change.

Now press F12 inside the browser. You'll see that he didn't find something. It's the instructions.js file, which we're asking him to read in that part that says <script>.

Then create a new file in notepad and just write:

 

                window.console.log( 5 * 8 );

 

Save this file as instructions.js ( js means JavaScript), in the same folder as umapagina.html and open the html again and press F12 again.

The instruction we gave was to display the result of 5 * 8, and it did.

 

First thing

 

The first of the coolest things a computer does is save a variable. Now we are going to leave the html aside and work on “instructions.js”.

Let's create a variable called x.

Replace the text with

 

                var x = “Fernando”;

                window.console.log(“My name is” + x);

               

Open it again and see.

He knows that “x” is Fernando.

It's like a box. If I need “Fernando”, the computer knows where the box where “Fernando” was kept is.

               

Now let's do something fancier.

Replace all text in our JavaScript file with:

 

                var name = prompt("Hi! What's your name?");

                alert("Your name is " + name);

 

Open the html again.

Cool huh? But let's do something even cooler.

 

second thing

 

The second thing is called a “conditional statement” and uses the word “if”. “If”.

Replace the text with:

 

                var name = prompt("Hi! What's your name?");

                if (name =="Fernando") {

                                alert("You're messy, but you're cool.");

                }

 

You don't need to open the file from outside to update, just press F5.

What we said was:

               

                Ask for the name and save it in the “name” variable.

                If (name is “Fernando”)

                               { warn "You're messy but you're nice". }

 

And the cool thing is that you can line up ifs for different situations, using “else”, that is, “in another case”.

Do like this:

               

                var name = prompt("Hi! What's your name?");

                if (name =="Fernando") {

                                alert("You're messy, but you're cool.");

                } else if (name == "Amanda") {

                                alert("Damn, you look pretty today!");

                } else if (name == "Charlie") {

                                alert("I think you're so cool");

                }             

 

Maybe you can use this to make your little brother smile.

Now, you may have noticed that if the name is anything other than one of these 3, the program doesn't know what to do. We can “else” without “if”, to say what it does in all other cases.

Complete our little program like this:

 

var name = prompt("Hi! What's your name?");

if (name =="Fernando") {

                alert("You're messy, but you're cool.");

} else if (name == "Amanda") {

                alert("Damn, you look pretty today!");

} else if (name == "Charlie") {

                alert("I think you're so cool");

} else {

                alert("I'm not an infinite program, but nice to meet you.");

}

 

Nenhum comentário:

Postar um comentário