2주차 사전스터디 wecode) JS

개발자 우니·2020년 5월 21일
0

사전스터디

목록 보기
1/2

🙋‍♀️Mission🙆‍♂️

- javascript에서 var, let, const를 이용해 변수를 선언하고 각각의 차이점을 조사해보세요

--The var keyword is used in pre-ES6 versions of JS.
--let is the preferred way to declare a variable when it can be reassigned.
--const is the preferred way to declare a variable with a constant value.

- template literal을 이용해 변수와 string을 동시에 작성해보세요

-- 를 이용해서 string과 변수 함께 ${ }
let a = '아이스크림';
let b = 1;
let c = 4;
let str = ${a} ${b+c}개 주세요.;
console.log(str); // 아이스크림 5개 주세요.

- for문과 while문을 반복문을 작성해보세요

--<for문>

const vacationSpots = ['Bali', 'Paris', 'Tulum'];

for (let i=0; i<vacationSpots.length; i++){
console.log("I would love to visit " + vacationSpots[i]);

}

I would love to visit Bali
I would love to visit Paris
I would love to visit Tulum
출력

--<while문>

const cards = ['diamond', 'spade', 'heart', 'club'];

let currentCard;
let times = 0;
while (currentCard !== 'spade') {
currentCard = cards[Math.floor(Math.random() * 4)];
times++
console.log(currentCard);
console.log(times);
}

spade가 안 나올때까지 (랜덤적으로) currentCard 출력, times도 값이 증가

- if와 else를 이용해 조건문을 작성해보세요

--
let tool = '';

let writingUtensil = tool || 'pen';
(let newVar = anotherVal || 'defaultVal' ; )

if (tool) {
let writingUtensil = tool;
} else {
let writingUtensil = 'pen';
}

console.log(The ${writingUtensil} is mightier than the sword.);
--'The pen is mightier than the sword.' 출력(tool =' ' (null)이라서 else로 진행)

let favoritePhrase = 'Love That!';

if (favoritePhrase === 'Love That!') {
console.log('I love that!');
} else {
console.log("I don't love that!");
}

=== 같은 의미
favoritePhrase === 'Love That!'? console.log('I love that!') (; 없음) : console.log("I don't love that!") ;

- array method 중 slice, splice, push, pop, filter, map 을 활영한 함수를 작성해보세요

--<.push() : array에 elements 첨가>
const chores = ['wash dishes', 'do laundry', 'take out trash'];
chores.push('mopping', 'brushing');
console.log(chores); = 위 2개 첨가

--<.pop() : array에서 끝에서부터 element 제거 >
const chores = ['wash dishes', 'do laundry', 'take out trash', 'cook dinner', 'mop floor'];
chores.pop();
console.log(chores); = 'mop floor' 제거됨

--<.shift() : array에서 처음부터 element 제거 >
const groceryList = ['orange juice', 'bananas', 'coffee beans', 'brown rice', 'pasta', 'coconut oil', 'plantains'];
groceryList.shift();
console.log(groceryList); = 'orange juice' 제거됨

--<.unshift() : array 처음에 elements 추가 >
groceryList.unshift('popcorn');
console.log(groceryList); = 'popcorn' 처음에 추가

--<.slice(0,3) : array 0 부터 2까지 추출 // non-mutating 기존 array 변형시키지 않음>
console.log(groceryList.slice(1,4));=['bananas'~'brown rice']

--<.splice() : array의 element 변경 >
.splice(위치index, 바꿀 element수, 'element)

const months = ['Jan', 'March', 'April', 'June'];
months.splice(1, 0, 'Feb');
// inserts at index 1
console.log(months);
// expected output: Array ["Jan", "Feb", "March", "April", "June"]

months.splice(4, 1, 'May');
// replaces 1 element at index 4
console.log(months);
// expected output: Array ["Jan", "Feb", "March", "April", "May"]

--<.filter() : 조건에 맞는 elements 만 array로 생성>
var filtered = [12, 5, 8, 130, 44].filter(function(element, index, array) {
return (element >= 10);
});

console.log(filtered); // [ 12, 130, 44 ] 출력

--<.map() : 기존 array를 새롭게 변경 >
const array1 = [1, 4, 9, 16];

// pass a function to map
const map1 = array1.map(x => x * 2);

console.log(map1);
// expected output: Array [2, 8, 18, 32]

--<.indexOf() :array에서 그 element 순번 알려줌 >
groceryList.indexOf('bananas'); = 1 출력

- switch & case & break

let athleteFinalPosition = 'first place';

switch (athleteFinalPosition) {
case 'first place' :
console.log('You get the gold medal!');
break;
case 'second place' :
console.log('You get the silver medal!');
break;
case 'third place' :
console.log('You get the bronze medal!');
break;
default :
console.log('No medal awarded.');
break;
}

  • 1주차 때 만든 자기소개 페이지에 javascript로 미니게임 코드를 넣어봐도 좋습니다.
profile
It’s now or never

0개의 댓글