@Sarah
2016-07-13T20:56:23.000000Z
字数 4000
阅读 1269
'''
//"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
console.log("Xiao Hui".length<122);
console.log("Goody Donaldson".length>8);
console.log(8*2>=16);
if( "myName".length >= 7 ) {
console.log("You have a long name!");
}
if( "myName".length >= 7 ) {
console.log("You have a long name!");
}
else {
console.log("You have a short name!");
}
if (1+1==22)
{
console.log("The condition is true")
}
else // "otherwise"
{
console.log("The condition is false")
}
if (10 == 10) {
console.log("You got a true!")
}
else {
console.log("You got a false!")
}
if (12 / 4 === "Ari".length) {
confirm("Will this run the first block?");
} else {
confirm("Or the second block?");
}
if("Jon".length * 2 / (2+1) != 1)
{
console.log("The answer makes sense!")
}
else {
console.log("Error Error Error")
}
// Below is an example of printing the remainder of 18/4 using modulo:
// console.log(18 % 4);
console.log(14 % 3);
console.log(99 % 8);
console.log(11 % 3);
if( 7%1===0 ) {
console.log("The first number is even");
} else {
console.log("The first number is odd");
}
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:
First 3 letters of "Batman"
"Batman".substring(0,3);
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".
// Be careful with the substring's letter positions!
"wonderful day".substring(3,7);
// Use console.log( ) to print out the substrings.
// Here is an example of the 1st to 4th letter of "JavaScript":
// console.log("JavaScript".substring(0,4));
console.log("January".substring(0,3));
console.log("Melbourne is great".substring(0,12));
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
// Declare a variable on line 3 called
// myCountry and give it a string value.
myCountry="kkkkkkkkkkkkk"
// Use console.log to print out the length of the variable myCountry.
console.log(myCountry.length );
// Use console.log to print out the first three letters of myCountry.
console.log(myCountry.substring(0,3) );