Anda di halaman 1dari 5

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 strin s! "Ryan", "4" and "What is your name?" "trin s are e#tremely useful as labels, names, and content for your pro rams.

Data Type III: Booleans The third type of data is a boolean $pronounced %bool&ee&un% and named after 'eor e (oole). * boolean can have only two values, true or false. You can use them in your code by makin statements that evaluate to true or false. For example! +. 10 > 3 evaluates to true ,. 5 < 4 is just cra-y talk, so it evaluates to false (ooleans are e#tremely useful because later they will let us run certain parts of our code only if certain conditions are true. .or e#ample, *T/s evaluate [the amount in your bank a ount! > 0 and will only ive you cash if the answer is true.

Using console.log You may have noticed that the interpreter doesn't print out every sin le thin it does. "o if we want to know what it's thinkin , we sometimes have to ask it to speak to us. onsole"lo#$% will take whatever is inside the parentheses and log it to the console below your code0that's why it's called onsole"lo#$%! This is commonly called printing out.

Comparisons 1e've learned about three data types! numbers, strin s, and booleans. 2et's learn about comparison operators and how they relate to data types. ist o! comparison operators! > 'reater than < 2ess than <& 2ess than or equal to >& 'reater than or equal to

&&& 3qual to '&& Not equal to

Decisions" #ecisions 4ice work on the comparisons! 4ow 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 i! statement or con#itional statement. Take a look at the code on the ri ht. The computer first looks at line +. If the condition $in this case, 100 < () is true, then it e#ecutes the code inside the curly braces )*. 5f the condition is false, it skips the code in the curly braces entirely and oes on to the ne#t line, which is line 6.

Computers are smart 'ood work with the if statements! 4ow, we're oin to ive your robot two options. Your robot has just come to a fork in the road. 5t is oin to check the pro ram that you wrote to decide which road it should o down. 5n code form, that looks kind of like this!
if $this on+ition is true% ) ,, +o this o+e * else ,, "other-ise" ) ,, +o this o+e instea+ *

Your robot starts on the first line. 5f the condition is true, it will e#ecute the code in the first set of curly braces, just like last time. This will skip the else block. (ut if the condition is false, the robot will skip the first block and e#ecute block after else. "o the robot e#ecutes one piece of code or the other, dependin on the condition in the first line. You're teachin it how to make decisions on its own! 7obots row up so fast. 5t seems like just yesterday that it was in robotic diapers.

$i#%lesson breat&er 1e've covered a lot of round so far! "o many new terms, so much synta#. 2et's take a breather and review. 1e have learned! '. Con!irm an# prompt

1e can make pop&up bo#es appear! onfirm$". am ok"%/ 0rom0t$"1re you ok?"%/ (. Data types a. numbers $e.g. 4"3, 134) b. strin s $e.g. "+o#s #o -oof'", "2a3a4 ri0t e50ert") c. booleans $e.g. false, 5 > 4) ). Con#itionals If the first condition is met, e#ecute the first code block. If it is not met, e#ecute the code in the else block. "ee the code on the ri ht for another e#ample.

$at& 1e saw basic math before. The basic math symbols we learned in school work here. 3ven the order in which the computer understands the math is the same as in school! Co#e! +. $ %! control order of operations ,. 6 and ,! multiplication and division 8. - and 7! subtraction and addition *xamples! +. 100,10 evaluates to +9 ,. "2ane""len#th 7 5 evaluates to : 8. 56$371% evaluates to ,9

$ore examples! 18 9 5 evaluates to , 13 9 8 evaluates to 6

$o#ulo an# i! + else "o why learn modulo; .or one thin , it's ood at testin divisibility. <onsider 30 9 10. 1hat does it return; There is nothin left over, so 0. =ow about : 9 3; *lso 0. (00 9 100; You uessed it....(! >ust kiddin , it's also 0. This is fun when we combine it with conditionals.

Substrings 1e've learned a few ways to manipulate numbers. 1hat about manipulatin strin s; "ometimes you don't want to display the entire strin , just a part of it. .or e#ample, in your 'mail inbo#, you can set it to display the first ?9 or so characters of each messa e so you can preview them. This preview is a substring of the ori inal strin $the entire messa e). Co#e! "some -or+""substrin#$5; y% where 5 is where you start choppin and y is where you finish choppin the ori inal strin . The number part is a little stran e. To select for the %he% in %hello%, you would write this! "hello"" substrin#$0; (%/ Think of there bein a marker to the left of each character, like this! 0-h-1-e-(-l-3-l-4-o-5. 5f you chop at 0 and a ain at ( you are left with just he. $ore examples! +. .irst 8 letters of %(atman% "<atman""substrin#$0;3% ,. .rom @th to 6th letter of %laptop% "la0to0""substrin#$3;=%

,-.I-B *S 1e have learned how to do a few thin s now! make strin s, find the len th of strin s, find what character is in the nth position, do basic math. 4ot bad for a day's work! To do more comple# codin , we need a way to 'save' the values from our codin . 1e do this by definin a variable with a specific, case&sensitive name. Ance you create $or #eclare) a variable as havin a particular name, you can then call up that value by typin the variable name. Co#e! 3ar 3ar>ame & +ata/