본문 바로가기
Web Dev

Intermediate Javascript

by YGSEO 2021. 1. 2.
728x90

Comparator and Equality

Three equal signs ("===") is also checking the datatypes are matching.(more strict)

Two equal signs doesn't care about the data types.

 


&& : AND

II : OR

 

function bmiCalculator (weight, height) {

    var interpretation =weight/(height*height);

   

    if(interpretation<18.5) {

    return("Your BMI is "+interpretation+", so you are underweight.");}




    if ((interpretation>=18.5)&&(interpretation<=24.9)){

        return("Your BMI is "+interpretation+", so you have a normal weight.");

    }

    if(interpretation>24.9){

        return("Your BMI is "+ interpretation+", so you are overweight.");

}

}

Finish a line with semi-colon.

Return text be embraced by parenthesis.


Leap year - multiple conditions

function isLeap(year) {
  if (year % 4 === 0) {
    if (year % 100 === 0) {
      if (year % 400 ===0) {
        return "Leap year.";
      } else {
        return "Not leap year.";
      }
    } else {
      return "Leap year.";
    }
  } else {
    return "Not leap year.";
  }
}

Javascript Arrray

var guestLiest = ['Angela','Jack','Pam','James'];

var guestName = prompt("What is you name?");

if (guestLiest.includes(guestName)){
  alert("Welcome!");
} else {
  alert("Sorry, maybe next time.")
}

array.includes: return Bool if the value in this array.

array.length: return length of array.


FizzBuzz (if else if, multiple conditions, increment, array push)

var output = []
count = 1
function fizzbuzz(){
    if (count % 3 === 0 && count % 5 === 0){
    output.push("FizzBuzz");
    }
    else if (count % 3 === 0) {
    output.push("Fizz");
    }
    else if (count % 5 === 0) {
    output.push("Buzz");
    }
    else {
    output.push(count);
    }
    count++;
    console.log(output)
}


Buying Lunch ( array's position )

function whoPaying(names){
  var numPeople = names.length;
  var randomposition = Math.floor(Math.random() * numPeople);
  var payer = names[randomposition];
  return payer + "is going to buy lunch today!."
}

FizzBuzz with while loop

var output = []
count = 1
function fizzbuzz(){

    while(count <= 100){
        
        if (count % 3 === 0 && count % 5 === 0){
        output.push("FizzBuzz");
        }
        else if (count % 3 === 0) {
        output.push("Fizz");
        }
        else if (count % 5 === 0) {
        output.push("Buzz");
        }
        else {
        output.push(count);
        }
        count++;
    }


    console.log(output)
}


For loop

Fibonacci

function finobacci(n){
  var output = [];

  if (n === 1){
    output = [0];
  }
  else if (n === 2){
    output = [0, 1];
  }
  else {
    output = [0, 1];
    
    for (var i=2; i < n; i++){
      output.push(output[output.length - 2 ]+output[output.length-1]);
      }
    }
  
  return output
  }

  fi = finobacci(10);
  console.log(fi);

console output

 

728x90

댓글