Javascript Day1

Jisu Lee·2023년 2월 3일
0

Javascript 스터디를 시작하였습니다. 두근두근거리는 마음으로 nomadcoders 강의를 수강하기로 하였습니다. Lecture 1과 Lecture 2 기록용 필기입니다.

#2.1~2.3 (Data Types)

let a = 5;
const b = 2;

const myName = "jisu";
let veryLongVariableName ="this is how it works";

console.log(a+b);
console.log(a*b);
console.log("hello "+ myName);
console.log(veryLongVariableName);

veryLongVariableName = "let can be changed";

console.log(veryLongVariableName);


/* const and let is similar, variables don't change in const, but let is for creating someone, you can update it later
we will never change 'const' but we can update 'let' variables
always use const, sometimes use let, never use var */

#2.4 (Booleans)

const amIFAT = true;
console.log(amIFAT);

const amISLIM = null;
console.log(amISLIM);
/*the output is null, has value the value is null*/

let something;
console.log(something);
/*the output would be undefined, does not have a value*/

#2.5 (Arrays)

const mon = "mon";
const tue ="tue";

const dayOfWeek = mon + tue;

console.log(dayOfWeek);
/* just a whole chunk of strings*/

const dayOfWeek2 = [mon, tue]; /*creating array of week*/
console.log(dayOfWeek2);
/* the output is "mon", "tue"*/

/* or to create an array we could do*/
const dayOfWeek3 = ["mon", "tue", "wed", "thu", "fri", "sat"];
console.log(dayOfWeek3[5]);
dayOfWeek3[5];
/*get the fifth of the array, starting with 0*/

dayOfWeek3[5]="saturday" /*to change what's in the array*/

//Add one more day to the array
dayOfWeek3.push("sun");
console.log(dayOfWeek3);

#2.6 (Objects)

const player = {
    name: "jisu",
    points: 10,
    fat: true,
};

console.log(player);
console.log(player.name);
console.log(player["name"]);

player.fat=false;
//the property gets updated

player.lastName="potato";
//add new property

player.points = player.points + 15;
//get something from the object and update

//console is also an object just like player.name, console.log looks similar

#2.7~2.8 (Functions)

function sayHello(nameOfPerson, age){
    console.log("Hello my name is "+ nameOfPerson + " and I'm " + age+ "years old.");
}


sayHello("jisu", 22);
sayHello("nico", 23);
sayHello("jennings", 24);

//sayHello function takes two arguments, the name of the player and their age

function을 활용한 간단한 계산기 만들기

function plus(firstNumber,secondNumber){
    console.log(firstNumber+secondNumber);
}

plus(2,3)

function divide(a,b){
    console.log(a/b);
}
divide(12,3);

function을 property 취급하기

const player = {
    name: "jisu",
    sayHello: function(otherPersonName){
        console.log("hello " + otherPersonName + " nice to meet you!");
    },
};

console.log(player.name);
player.sayHello("lynn");

#2.11 (Functions)

const age = 96;
function calculateKrAge(ageOfForeigner){
    return ageOfForeigner+2;
}

const krAge = calculateKrAge(age);
//by using return, we replace the line that recalled the final result of the function

console.log(krAge);

console.log 대신 return을 사용한 계산기

const calculator = {
    plus: function (a,b){
        return a+b;
    },
    minus: function (a,b){
        return a-b;
    },
    times: function (a,b){
      	return a*b;
    },
  	divide: function (a,b){
      	return a/b;
    },
  	power: function (a,b){
      	console.log("hell0");
      	return a**b;
      	console.log("bye bye");
      	//the output returns hello and a**b but does not return bye bye as the function finishes its job at 'return'
    },
};


const plusResult = calculator.plus(2,3);
const minusResult = calculator.minus(plusResult,10);
console.log(plusResult);
console.log(minusResult);

#2.13~2.15 (Conditionals)

const age = prompt("How old are you?"); 
//does not use this anymore because we cannot modify this message box, pause the javascript execution until the user inputs something
console.log(age);

console.log(typeof age);
//looks like a number but it is actually a string

console.log(typeof age, typeof parseInt (age));
//change the string into a number, we use parseInt
//if we try to take a string and put it into parseInt the output would be NaN (not a number)

isNaN as condition (using if statement)

const age = parseInt(prompt("How old are you?")); 

console.log(isNaN(age));
//will return boolean, false=age is a number, true=age is NaN

if(isNaN(age)){
   console.log("Please write a number");
} else{
    console.log("Thank you for writing your age");
}  

using elseif for more conditions

const age = parseInt(prompt("How old are you?")); 


if(isNaN(age) || age<0){
   console.log("Please write a positive number");
} else if(age<18){
    console.log("You are too young.");
} else if(age>=18 && age <= 50){
    console.log("You can drink");
} else if(age > 50 && age <=80){
    console.log("You should exercise");
} else if (age ===100){
    console.log("wow you are wise");
} else {
    console.log("Do whatever you want");
}

//and operator is &&, or operator is ||, equal is ===, not equal is !==

0개의 댓글