[JavaScript] Statements

=====if else if

var today = new Date().getDay();

if (today == 5){
  console.log('It is Friday');
}

else if (today ==0|| today ==6) {
  console.log('No working on the weekend!');
}

else {
  console.log('working working working!');
}

=====switch

var today = new Date().getDay();

switch (today){
  case 0: console.log('Today it is Sunday'); break;
  case 6: console.log('Today it is Saturday'); break;
  default: console.log('Today it is ' + today);
}

or

switch (today){
  case 0:
  case 6: console.log('On the weekend!');break;
  default: console.log('Just go back to work!');
}

=====for 

for (var i = 0; i < 7; i++){
  console.log(i);
}

=====for in

var myList = ['A', 2, 'C', 'd', 'x']

for (var i in myList){
  console.log(myList[i]);
}

or

var myList = [
  {id:'ABC',pw:123},
  {id:'DEF',pw:456},
  {id:'GHI',pw:789},
]

for (var i in myList) {
  var myObject = myList[i];
  console.log(myObject.id + ' : ' + myObject.pw);
}

=====while

var today = new Date().getDay();
var index = 0;

while (index <= 6) {
    if (index == today) {
        if (index == 0) {
            console.log("Today is Sunday");
        }
        else {
            console.log("Today is " + index);
        }
    }
    index++;
}

=====do while

var index = 1;

do {
    if (index % 2 != 0) {
        console.log(index)
    }
    index++;
} while (index <= 10);

=====continue

var list = [
    { id: 'AB', score: '89' },
    { id: 'CD', score: '59' },
    { id: 'EF', score: '20' },
    { id: 'GH', score: '34' }
];

for (var i in list) {
    if (list[i].score <= 60) { continue; }
    console.log('Exam pass ID is ' + list[i].id + ', the score is ' + list[i].score);
}

=====with

var list = [
    { id: 'AB', score: '89' },
    { id: 'CD', score: '59' },
    { id: 'EF', score: '99' },
    { id: 'GH', score: '34' }
];

for (var i in list) {
    with (list[i]) {
        if (score <= 60) { continue; }
        console.log('Exam pass ID is ' + list[i].id + ', the score is ' + list[i].score);
    }
}

留言

這個網誌中的熱門文章

[Python] raw_input() function

[Golang] for