JavaScript | 자바스크립트 | CONTROL FLOW

JavaScript | 자바스크립트 | CONTROL FLOW

More on Control Flow in JS | Codecademy
When we tell a program what order to do things in, we're using something called control flow. You already know about 'if' statements, but in this course, we'll expand our knowledge!


One. The Story So Far: If, Else, and Loops
1/14 If / else
File1 _ script.js
var isEven = function(number) {
  // Your code goes here!
  if (number % 2 === 0) {
    return true;  
  } else {
    return false;  
  }
};
isEven(5);

2/14 If / else if / else
File1 _ script.js
var isEven = function(number) {
  // Your code goes here!
  if (number % 2 === 0) {
    return true;  
  } else if (isNaN(number)) {
    return "Your input is not a number.";
  } else {
    return false;  
  } 
};


isEven("7unicorns");

3/14 For or while
File1 _ script.js
// Write your loop below
var cond = true;
while (cond) {
    console.log("Hello World!");
    cond = false;

}

4/14 Sneak preview: the switch statement
File1 _ script.js
var lunch = prompt("What do you want for lunch?","Type your lunch choice here");

switch(lunch){

  case 'sandwich':
    console.log("Sure thing! One sandwich, coming up.");
    break;
  case 'soup':
    console.log("Got it! Tomato's my favorite.");
    break;
  case 'salad':
    console.log("Sounds good! How about a caesar salad?");
    break;
  case 'pie':
    console.log("Pie's not a meal!");
    break;
  default:
    console.log("Huh! I'm not sure what " + lunch + " is. How does a sandwich sound?");
}

Next: Introducing the Switch Statement


| 코딩 공부, 매일 매일 거르면 안되겠습니다. 중간에 끊기니까 흐름도 깨지고, 이해하는 것도 더욱 어려워지네요. 뭐든지 그렇겠지만, '꾸준함' 이야말로 벽 너머로 갈 수 있는 'KEY' 라는 생각을 다시 한번 하게 됩니다.