Skip to content

Instantly share code, notes, and snippets.

@khanh96le
Last active July 20, 2016 04:11
Show Gist options
  • Save khanh96le/2f16178e1cb0f7ccf36053172ae8e75f to your computer and use it in GitHub Desktop.
Save khanh96le/2f16178e1cb0f7ccf36053172ae8e75f to your computer and use it in GitHub Desktop.

// What is an expression? // --> a fragment of code that produces a value is calls an expression

VARIABLE:
var ten = 10;
console.log(ten * ten);

var mood = "light";
console.log(mood);
mood = "dark";
console.log(mood);

var debt = 100;
debt = debt - 40;
console.log(debt);

var one = 1, two =2;
console.log(one + two);


// What is the environment?
// --> the colletion of variables and their values that exist at a given 
// time is calls the environment


FUNCTION
function is a type of value. In the default evironment, there are some 
values has wrapped, they are called function 
alert("Hello Guys!"); 
alert is a function (or a variable)
"Hello Guys!" is an argument

confirm("Shall we, then?");
console.log(confirm("are you ok?"));
-> true: if choose ok
-> false: if choose cancel

promt("Enter your name", "...");
-> return a string

var theNumber = Number(prompt("Pick a number", ""));
alert("Your Number is the square root of " + theNumber * theNumber);

CONTROL FLOW
var theNumber = Number(prompt("Pick a number", ""));
if (!isNaN(theNumber))
 alert("Your Number is the square root of " + theNumber * theNumber);
else 
 alert("Why don't you give me a number?");

LOOP
var result = 1;
var counter = 0;
while (counter < 10) {
    result = result * 2;
    counter = counter + 1;
}
console.log(result);

// for
var result = 1;
for (var counter = 0; counter < 10; counter = counter +1) {
    result = result * 2;
}
console.log(result);

BREAK
for (var counter = 1; ; counter++){
    if (counter % 7 == 0)
      break;
}
console.log(counter);

SWITCH
switch(prompt("what is your mood?")){
  case "wonderful":
    console.log("you should go shopping");
    break;
  case "good":
    console.log("you should go fishing");
    break;
  case "normal":
    console.log("you should go eating");
    break;
  case "bad":
    console.log("you should go sleeping");
    break;
  default:
    console.log("you should go making love");
    break;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment