2014年9月24日 星期三

[study0003]JavaScript學習筆記

2013/12/17

Anything after a // is called a comment. 註解

confirm("hahha");   //跳出確認視窗

prompt("what is your name?"); //輸入視窗 
Use the prompt command to ask the user where they are from.

Data Types I & II: Numbers & Strings

字串 strings are sequences of characters, like the letters a-z, spaces, and even numbers. These are all strings: "Ryan""4" and "What is your name?"
To check the length of something, we type"string".length

布林A boolean can have only two values, true or false. 回傳true  或false
ex: 10 > 3 evaluates to true

Using console.log
console.log() will take whatever is inside the parentheses and log it to the consolebelow your code—that's why it's calledconsole.log()!  // 印出
This is commonly called printing out.

2013/12/18

List of comparison operators:
  • > Greater than
  • < Less than
  • <= Less than or equal to
  • >= Greater than or equal to
  • === Equal to   等於
  • !== Not equal to   不等於

if (this condition is true) 
{
    // do this code
}
else // "otherwise"
{
    // do this code instead
}

2014/01/05
變數
Code:

var varName = data;
Example:

a. numbers
a. var myName = "Leng";
b. strings -
b. var myAge = 30;
c. booleans
c. var isOdd = true;
Substrings
Code:
"some word".substring(xy) where xis where you start chopping and y is where you finish chopping the original string.


For example:

var myName = "Steve Jobs";

myName.substring(0,5)

 Steve

變數大小寫有差

確認視窗: 取消/確定

confirm("this is my code life back");

Remember: = is for assignment, and === is to check if things are equal!

2014/03/09
Here's an example of a function:


var sayHello = function(name) {
    console.log('Hello ' + name);
};

we declare a function using var, and then give it a name sayHello.
we use the function keyword to tell the computer that you are making a function.
The code in the parentheses括號 is called a parameter參數
 write your block of reusable code between }
Every line of code in this block must end with a ;.


var functionName = function( ) {
    // code code code
    // code code code
    // (more lines of code)
};

2014/03/11


Don't Repeat Yourself (D.R.Y)
The D.R.Y. principle is really important in programming. 
Return keyword
when we call a function, we don't always want to just print stuff. Sometimes, we just want it to return a value. 
The return keyword simply gives the programmer back the value that comes out of the function.

2014/03/16
Variables defined outside a function are accessible anywhere once they have been declared. They are called global variables and their scope is global.
Variables defined inside a function are local variables. They cannot be accessed outside of that function.

2014/04/10


Here's a list of of comparison operators:
  • > Greater than
  • < Less than
  • <= Less than or equal to
  • >= Greater than or equal to
  • === Equal to
  • !== Not equal to

Here's an example of if/else syntax:

if (condition1) {
    return "some string";
}
else {
    return "another string";
}

alert ("Let's go down the first road!"); //跳出警告視窗


2014/08/24

Math and the modulo
Let's meet an interesting symbol called modulo. When % is placed between two numbers, the computer will divide the first number by the second, and then return the remainder of that division.
So if we do 23 % 10, we divide 23 by 10 which equals 2 with 3 left over. 
So 23 % 10evaluates to 3. //餘數
More examples:

17 % 5 evaluates to 2

13 % 7 evaluates to 6.
Variables
Code:

var varName = data;
Data types
  • strings (e.g. "dogs go woof!")
  • numbers (e.g. 410)
  • booleans (e.g. false5 > 4)
Variables
We store data values in variables. We can bring back the values of these variables by typing the variable name.
Manipulating numbers & strings
  • comparisons (e.g. ><=)
  • modulo (e.g. %)
  • string length (e.g. "Emily".length;)
  • substrings (e.g. "hi".substring(0, 1);)
console.log( )
Prints into the console whatever we put in the parentheses.
Confirm
confirm("This is a example.");

2014/09/25

Function

we declare a function using var, and then give it a name sayHello.
The code in the parentheses 括號  is called a parameter變數. It's a placeholder word that we give a specific value when we call the function. 
  1. Then write your block of reusable code between }. Every line of code in this block must end with a ;.
You can run this code by "calling" the function

var sayHello = function(name) {
    console.log('Hello ' + name);
};

like this:
sayHello("Emily");
Calling this function will print out Hello Emily.

var functionName = function( ) {
    // code code code
    // code code code
    // (more lines of code)
};
  1. The var keyword declares a variable named functionName.
  2. The keyword function tells the computer that functionName is a function and not something else.
  3. Parameters go in the parentheses. The computer will look out for it in the code block.
  4. The code block is the reusable code that is between the curly brackets }. Each line of code inside } must end with a semi-colon.
  5. The entire function ends with a semi-colon.
To use the function, we call the function by just typing the function's name, and putting a parameter value inside parentheses after it. The computer will run the reusable code with the specific parameter value substituted into the code.
2014/10/4

Don't Repeat Yourself (D.R.Y)
Return keyword
ometimes, we just want it to return a value. 

or example, we can have the following function:
var areaBox = function(length, width) {
    return length * width;
};

Variables defined outside a function are accessible anywhere once they have been declared. They are called global variablesand their scope is global.

Variables defined inside a function arelocal variables. They cannot be accessed outside of that function.

Remember to use prompt to ask the user a question, like so:
var example = prompt("Question");


if (computerChoice >0 && computerChoice <0.33)
{
    computerChoice === "rock";
}

var compare = function(choice1,choice2){
    
    if(choice1 === choice2)
    {
        return "The result is a tie!";
    }

};



compare(userChoice,computerChoice);