[关闭]
@Sarah 2016-07-13T20:56:23.000000Z 字数 4000 阅读 1269

Java Script

'''
//"sarah"
3+4
//commant

"length".length

confirm("hahaha");//弹出窗口让你确定hahaha

prompt("What is your name?");// 让你输入你从哪里来

"String".length
I'm coding like a champ".length>10//这个是Boolean判断TF

console.log(2*5)
console.log("Hello") //print it out
'''

So far we've learned about three data types:

strings (e.g. "dogs go woof!")
numbers (e.g. 4, 10)
booleans (e.g. false, 5 > 4)
Now let's learn more about comparison operators.

List of comparison operators:

Greater than
< Less than
<= Less than or equal to
= Greater than or equal to
=== Equal to
!== Not equal to

  1. console.log("Xiao Hui".length<122);
  2. console.log("Goody Donaldson".length>8);
  3. console.log(8*2>=16);
  1. if( "myName".length >= 7 ) {
  2. console.log("You have a long name!");
  3. }
  1. if( "myName".length >= 7 ) {
  2. console.log("You have a long name!");
  3. }
  4. else {
  5. console.log("You have a short name!");
  6. }
  1. if (1+1==22)
  2. {
  3. console.log("The condition is true")
  4. }
  5. else // "otherwise"
  6. {
  7. console.log("The condition is false")
  8. }
  1. if (10 == 10) {
  2. console.log("You got a true!")
  3. }
  4. else {
  5. console.log("You got a false!")
  6. }
  1. if (12 / 4 === "Ari".length) {
  2. confirm("Will this run the first block?");
  3. } else {
  4. confirm("Or the second block?");
  5. }
  1. if("Jon".length * 2 / (2+1) != 1)
  2. {
  3. console.log("The answer makes sense!")
  4. }
  5. else {
  6. console.log("Error Error Error")
  7. }
  1. // Below is an example of printing the remainder of 18/4 using modulo:
  2. // console.log(18 % 4);
  3. console.log(14 % 3);
  4. console.log(99 % 8);
  5. console.log(11 % 3);
  1. if( 7%1===0 ) {
  2. console.log("The first number is even");
  3. } else {
  4. console.log("The first number is odd");
  5. }

Substrings

We've learned a few ways to manipulate numbers. What about manipulating strings?

Sometimes you don't want to display the entire string, just a part of it. For example, in your Gmail inbox, you can set it to display the first 50 or so characters of each message so you can preview them. This preview is a substring of the original string (the entire message).

Code:

"some word".substring(x, y) where x is where you start chopping and y is where you finish chopping the original string.

The number part is a little strange. To select for the "he" in "hello", you would write this:

"hello". substring(0, 2);
Each character in a string is numbered starting from 0, like this:

0 1 2 3 4
| | | | |
h e l l o
The letter h is in position 0, the letter e is in position 1, and so on.

Therefore if you start at position 0, and slice right up till position 2, you are left with just he

More examples:

  1. First 3 letters of "Batman"
    "Batman".substring(0,3);

  2. From 4th to 6th letter of "laptop"
    "laptop".substring(3,6);

Instructions
Find the 4th up to and including the 7th letter of the string "wonderful day".

  1. // Be careful with the substring's letter positions!
  2. "wonderful day".substring(3,7);
  1. // Use console.log( ) to print out the substrings.
  2. // Here is an example of the 1st to 4th letter of "JavaScript":
  3. // console.log("JavaScript".substring(0,4));
  4. console.log("January".substring(0,3));
  5. console.log("Melbourne is great".substring(0,12));
  6. console.log("Hamburgers".substring(3,10));

Variables
We have learned how to do a few things now: make strings, find the length of strings, find what character is in the nth position, do basic math. Not bad for a day's work!

To do more complex coding, we need a way to 'save' the values from our coding. We do this by defining a variable with a specific, case-sensitive name. Once you create (or declare) a variable as having a particular name, you can then call up that value by typing the variable name.

Code:

var varName = data;

Example:

a. var myName = "Leng";
b. var myAge = 30;
c. var isOdd = true;

Instructions
Create a variable called myAge and type in your age.

var myAge=25;
console.log(myAge);

More Variable Practice
We have seen how to create a variable. But how do we use it? It is useful to think that any time you type the variable's name, you are asking the computer to swap out the variable name and swap in the value of the variable.

For example:

var myName = "Steve Jobs";

myName.substring(0,5)

Look at the second line above. You have asked the computer to swap out myName and swap in Steve Jobs, so

myName.substring(0,5)

becomes

"Steve Jobs".substring(0,5)

which evaluates to Steve.

Another example

var myAge = 120;

What is

myAge % 12 ? See the hint to check your answer.

So the variable stores the value of the variable, whether that is a number or a string. As you will see soon, this makes writing long programs much easier!

Instructions

  1. // Declare a variable on line 3 called
  2. // myCountry and give it a string value.
  3. myCountry="kkkkkkkkkkkkk"
  4. // Use console.log to print out the length of the variable myCountry.
  5. console.log(myCountry.length );
  6. // Use console.log to print out the first three letters of myCountry.
  7. console.log(myCountry.substring(0,3) );
添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注