본문 바로가기
Programming/JavaScript

[CodeAcademy] 함수

// Function

function getReminder() {
  console.log('Water the plants.');
}

function greetInSpanish() {
  console.log('Buenas Tardes');
}

function sayThanks(){
  console.log('Thank you for your purchase! We appreciate your business.');
}
sayThanks();
sayThanks();
sayThanks();

function sayThanks(name) {
  console.log('Thank you for your purchase, ' + name + '! We appreciate your business.');
}

sayThanks('Cole');

function makeShoppingList(item1='milk', item2='bread', item3='eggs'){
  console.log(`Remember to buy ${item1}`);
  console.log(`Remember to buy ${item2}`);
  console.log(`Remember to buy ${item3}`);
}

function monitorCount(rows, columns){
  return rows * columns;
};
const numOfMonitors = monitorCount(5, 4);
console.log(numOfMonitors);


function monitorCount(rows, columns) {
  return rows * columns;
};
function costOfMonitors(rows, columns) {
  return monitorCount(rows, columns) * 200;
};
const totalCost = costOfMonitors(5, 4);
console.log(totalCost);


const plantNeedsWater1 = function(day) {
  if (day === 'Wednesday') {
    return true
  } else {
    return false
    };
};
console.log(plantNeedsWater1('Tuesday'));

const plantNeedsWater2 = (day) => {
  if (day === 'Wednesday') {
    return true;
  } else {
    return false;
  }
};

const plantNeedsWater = day => day === 'Wednesday' ? true : false;
// 함수 구조 요약 ))))  const 함수이름 = (입력값) => {}; 


/// Scope

// 함수 입력값에 default 값으로 city = city 같이 하면 이 값은 외부에 선언된 const city가 아닌 자기 자신을 참조하여 undefined됨
const city = 'New York City';
const logCitySkyline = () => {
  let skyscraper = 'Empire State Building';
  return 'The stars over the ' + skyscraper + ' in ' + city;
};
console.log(logCitySkyline());

const satellite1 = 'The Moon'
const galaxy1 = 'The Milky Way'
const stars1 = 'North Star'
const callMyNightSky1 = () => {
  return 'Night Sky: ' + satellite1 + ', ' + stars1 + ', and ' + galaxy1; 
};
console.log(callMyNightSky1());

const logVisibleLightWaves1 = () => {
  const lightWaves = 'Moonlight';
  console.log(lightWaves);
};
logVisibleLightWaves2();
// global 영역의 let 변수 선언은 후에 재 배치되는 특성으로 코드가 꼬일 수 있으니 let 선언은 가능한 블록 안에서 하기
const satellite = 'The Moon';
const galaxy = 'The Milky Way';
let stars = 'North Star';

const callMyNightSky = () => {
  stars = 'Sirius';
	return 'Night Sky: ' + satellite + ', ' + stars + ', ' + galaxy;
};
console.log(callMyNightSky());
console.log(stars);
// 블럭 안에서만 쓰는 변수는 그 안에서만 선언해서 메모리 효율 높임

const logVisibleLightWaves = () => {
  let lightWaves = 'Moonlight';
	let region = 'The Arctic';
  if (region === 'The Arctic') {
    let lightWaves = 'Northern Lights';
    console.log(lightWaves);
  } else {
  };
  console.log(lightWaves);
};
logVisibleLightWaves();
728x90
반응형

'Programming > JavaScript' 카테고리의 다른 글

[CodeAcademy] object, this, getter, setter  (1) 2026.04.08
[CodeAcademy] Array, loop, Iterators  (0) 2026.04.08
[CodeAcademy] 기본 문법, 변수 선언, 조건문  (0) 2026.04.08
Tap(for)  (0) 2023.11.07
Scroll  (0) 2023.11.07