๐Ÿ’ฏ TIL) JavaScript ์–ด๋ ค์› ๋˜ Repl.it ๋ฌธ์ œ ์ •๋ฆฌ (2)

Solmiiยท2020๋…„ 6์›” 1์ผ
0

JavaScript

๋ชฉ๋ก ๋ณด๊ธฐ
18/24
post-thumbnail

โšพ๏ธ Object

์ฒ˜์Œ ํ’€์ด

function getData(salesArr, reviewArr, likeArr) {
  for(i = 0; i < salesArr.length; i+=2) {
      salesArr[i][1] + salesArr[i+1][1];
    }
  let objData = {
  };
  return objData;
}
                                 
salesArr1 = [["20190401", 34], ["20190402", 23], ["20190403", 29]];
reviewArr1 = [["20190328", 3], ["20190401", 0], ["20190403", 1]];
likeArr1 = [["20190328", 98], ["20190401", 102], ["20190403", 125]];

....?

๋ญ”์ง€ ์•„์˜ˆ ๊ฐ๋„ ์•ˆ์˜จ๋‹ค.....ใ…‹ใ…‹ใ…‹ใ…‹ใ…‹ใ…‹ใ…‹ใ…‹ใ…‹ใ…‹

์ด๋Ÿฐ ์ฝ”๋“œ ์ณ๋†“๊ณ  ๋ฉ˜ํƒˆํ„ฐ์ ธ์„œ ํ—ค๋กฑํ—ค๋กฑ ํ•˜๊ณ  ์žˆ๋‹ค๊ฐ€....
์ง€๋‚˜๊ฐ€์‹œ๋˜ ์Šนํ˜„๋‹˜์ด ํžŒํŠธ(์น˜๊ณค ๋งŽ์ด ๊ธด ํžŒํŠธ)๋ฅผ ๋˜์ ธ์ฃผ์…จ๋‹ค!!!

๋‚˜์˜ ํ•ด๊ฒฐ ๋ฐฉ๋ฒ•

function getData(salesArr, reviewArr, likeArr) {
  let sumAmount = 0;
  let sumReview = 0;
  let sumLike = 0; // ์ดํ•ฉ ๋ณ€์ˆ˜ ๋จผ์ € ์„ ์–ธ!
  
  for(let i = 0; i < salesArr.length; i++) {
      sumAmount += salesArr[i][1]; // for ๋ฌธ ๋Œ๋ฆฌ๋ฉด์„œ ์„ ์–ธํ•œ ๋ณ€์ˆ˜์— ํ•„์š”ํ•œ ๊ฐ’๋งŒ ์ถ”๊ฐ€
    }
  for(let i = 0; i < reviewArr.length; i++) {
      sumReview += reviewArr[i][1];
    }
  for(let i = 0; i < likeArr.length; i++) {
      sumLike += likeArr[i][1];
    }
    
  let objData = { // object์— ์ดํ•ฉ๊ณ„ ๊ฐ’ ๋„ฃ๊ธฐ!
    sumAmount: sumAmount,
    sumReview: sumReview,
    sumLike: sumLike,
  };
  return objData;
}

salesArr1 = [["20190401", 34], ["20190402", 23], ["20190403", 29]];
reviewArr1 = [["20190328", 3], ["20190401", 0], ["20190403", 1]];
likeArr1 = [["20190328", 98], ["20190401", 102], ["20190403", 125]];

getData(salesArr1, reviewArr1, likeArr1);


์„ฑ๊ณต!!!!!!!!!!!!!!!!! ์œผ์—‰์—‰ ใ… ใ… ใ… ใ… ใ… ใ… ใ… 

ํฌ์ธํŠธ๋Š” ๊ฐ ๋ฐฐ์—ด์˜ ์ดํ•ฉ์„ ์ €์žฅํ•  ๋ณ€์ˆ˜๋ฅผ ๋จผ์ € ์„ ์–ธํ•˜๊ณ , for๋ฌธ์œผ๋กœ ๋ฐฐ์—ด์•ˆ์˜ ๋ฐฐ์—ด์— index๋กœ ์ ‘๊ทผํ•ด์„œ ๋จผ์ € ์„ ์–ธํ•œ ๋ณ€์ˆ˜์— ์ฐจ๊ณก์ฐจ๊ณก ์ง‘์–ด๋„ฃ๋Š”๊ฒƒ!


์ฒ˜์Œ ํ’€์ด

const getCookie = () => {
  const name = "wecode_cookie";
  const cookies = document.cookie.split(";");
  
  for(let i in cookies) {
    if(cookies[i].includes(name) === true) {
        return cookies[i].slice(name.length+2,cookies[i].length);
    }
  }
}

getCookie();

cookie ๊ฐ’ ๊ฐ€์ ธ์˜ค๋Š”๊ฑด ๋ธ”๋กœ๊ทธ๋ฅผ ๋งŽ์ด ์ฐธ๊ณ ํ–ˆ๊ณ , string์„ ์ชผ๊ฐœ๋Š”๊ฑด ํ˜ผ์ž ์‹œ๋„ํ–ˆ๋Š”๋ฐ ํ•œ๋ฒˆ๋งŒ์— OK!!

์—ฌ๊ธฐ์„œ ํฌ์ธํŠธ๋Š” localStorage๋‚˜ sessionStorage๋Š” ๊ทธ๋ƒฅ key๊ฐ’์œผ๋กœ ์ ‘๊ทผํ•˜๋ฉด ๋ฐ”๋กœ value๋ฅผ ๋ฑ‰์–ด๋‚ด๋Š”๋ฐ ๋ฐ˜ํ•ด, cookie๋Š” cookie key=value ์ •๋ณด๋ฅผ ํ•˜๋‚˜์˜ string์œผ๋กœ ์ค„์ค„์ค„ ๋ฑ‰์–ด๋‚ธ๋‹ค๋Š”๊ฒƒ....๐Ÿ˜ต

๋‹จ, ๊ฐ key=value ์Œ ๋ผ๋ฆฌ ; ์œผ๋กœ ๊ตฌ๋ถ„ํ•˜๊ธฐ ๋•Œ๋ฌธ์— ์ด ; ๋ฅผ ๊ธฐ์ค€์œผ๋กœ string์„ ์ชผ๊ฐœ์„œ ๋ฐฐ์—ด๋กœ ๋ฐ˜ํ™˜ํ•œ ๋’ค, cookies[i] ๋กœ ๊ฐ cookie ๊ฐ’์— ์ ‘๊ทผํ–ˆ๋‹ค.

๋ฐฐ์—ด๋‚ด์˜ ๋ชจ๋“  i ๊ฐ’์„ ํ™•์ธํ•ด์•ผ ํ•˜๋ฏ€๋กœ for in ๊ตฌ๋ฌธ์„ ์‚ฌ์šฉํ–ˆ๊ณ  ์ฐพ์•„์•ผ ํ•˜๋Š” key ๊ฐ’์€ "wecode_cookie" ๋Š” ๋”ฐ๋กœ ๋ณ€์ˆ˜๋กœ ์ง€์ •ํ•ด์„œ for๋ฌธ๊ณผ slice ๋ฉ”์„œ๋“œ์— ์ด์šฉํ–ˆ์Œ!

slice ๋ฉ”์„œ๋“œ๋Š” string ๊ณผ์ œ ํ•ด๊ฒฐํ• ๋•Œ ์„œ์šธํŠน๋ณ„์‹œ ๋•Œ๋ฌธ์— ๋œํ†ต ๋‹นํ•ด์„œ ๊ทธ๋Ÿฐ์ง€ ์ˆ˜์›”ํ•˜๊ฒŒ ํ•ด๊ฒฐํ–ˆ๋‹คใ…‹ใ…‹ใ…‹

< ์ฐธ๊ณ ํ•œ ๋ธ”๋กœ๊ทธ : http://magic.wickedmiso.com/72 >


๐Ÿšด๐Ÿปโ€โ™€๏ธ array methods 1

์ฒ˜์Œ ํ’€์ด

let numsArr = [100, 9, 30, 7];

const moreThan100 = nums => 
  nums.map( i => { if (i < 100) {
    return false;
  }
  return true;
});

moreThan100(numsArr); // [ true, false, false, false ]

์ ์ ....๊ตฌ๊ธ€๋ง๊ณผ ๋ธ”๋กœ๊ทธ์— ์˜์ง€ํ•˜๊ฒŒ ๋œ๋‹ค... ๋‚˜ ์ด๋ž˜๋„ ๋˜๋Š”๊ฑธ๊นŒ....๐Ÿ˜‚?

์ด๋ฒˆ ๋ฌธ์ œ๋Š” ์ฒ™ ๋ณด๊ณ  nums.map ์— i < 100 ์กฐ๊ฑด์œผ๋กœ(ํ˜น์€ i โ‰ค 100์œผ๋กœ) if ๋ฌธ์„ ๋Œ๋ฆฌ๋ฉด ๋˜๊ฒ ๊ตฌ๋‚˜ ์‹ถ๊ธด ํ–ˆ์œผ๋‚˜...
arrow function ์•ž์—์„œ if ์กฐ๊ฑด์„ ๋„ฃ๋Š”๊ฒŒ ๋‚ฏ์„ค๊ณ  ์–ด๋ ค์› ๋‹ค!

Array.map()์€ ์•„์ง๋„ ํ—ท๊ฐˆ๋ ค์„œ.... forEach() ๋ž‘ ๊ฐ™์ด ์ด๋ฒˆ์ฃผ๋‚ด๋กœ ๋ฝ€๊ฐœ๊ธฐ์— ์ •๋ฆฌํ•  ์˜ˆ์ •!!!

< ์ฐธ๊ณ ํ•œ ๋ธ”๋กœ๊ทธ : https://www.zerocho.com/category/JavaScript/post/5acafb05f24445001b8d796d >

model solution

const moreThan100 = nums => {
  return nums.map(el => el > 100);
}

์˜ˆ...?

oh ์ƒ์ƒ๋„ ๋ชปํ•œ ์ฝ”๋“œ oh

๊ทธ๋ž˜์„œ ์ด๊ฒŒ ๋ญ” ๋ง?

( el > 100 ) ์€ boolean type ์ด๋‹ˆ๊นŒ ๊ฐ’์ด true ์•„๋‹ˆ๋ฉด false ๋กœ ๋‚˜์˜จ๋‹ค!

์ฆ‰ ์œ„ ์ฝ”๋“œ๋Š” ํ•œ ์ค„๋กœ return true/false ์ธ ๊ฒƒ.....!

์ด๋ ‡๊ฒŒ ๊ฐ„๋‹จํ•˜๊ฒŒ ์ž‘์„ฑํ•  ์ˆ˜ ์žˆ๋Š”๊ฑธ.... ๊ตณ์ด if๋ฌธ์œผ๋กœ ์ค„์ค„์ค„ ๊ธธ๊ฒŒ๋„ ์ ์—ˆ๋„คใ… ใ… ใ… ใ…  ๋ฐ˜์„ฑ๋ฐ˜์„ฑ....๐Ÿ˜ญ


๐Ÿšต๐Ÿปโ€โ™€๏ธ array methods 2

์ฒ˜์Œ ํ’€์ด

let birthDay = ['2019-03-21', '2019-04-21', '2019-05-21'];

const formatDate = dates =>
  dates.map ( i => { for (let i in dates) {
    return `${dates[i].slice(0, 4)}๋…„ ${dates[i].slice(5, 7)}์›” ${dates[i].slice(8, 10)}์ผ`;
  }
})

formatDate(birthDay); // [ '2019๋…„ 03์›” 21์ผ', '2019๋…„ 03์›” 21์ผ', '2019๋…„ 03์›” 21์ผ' ]

birthDay ๋ฐฐ์—ด์˜ [0]๋ฒˆ์งธ ๊ฐ’์ธ 2019๋…„ 03์›” 21์ผ ๊ฐ’์ด ๋‹ค๋ฅธ ๋ชจ๋“  ๋ฐฐ์—ด index ๊ฐ’์— ๋“ค์–ด๊ฐ€๋ฒ„๋ฆฐ๋‹ค....

๊ทธ๋ž˜์„œ [ '2019๋…„ 03์›” 21์ผ', '2019๋…„ 03์›” 21์ผ', '2019๋…„ 03์›” 21์ผ' ] ๊ฐ™์€ ๊ฒฐ๊ณผ๊ฐ€ ์ถœ๋ ฅ๋จ.....

๋ฐฐ์—ด์— dates.map ์„ ๋Œ๋ฆฌ๋Š” ๊ณผ์ •์—์„œ dates ๋ฐฐ์—ด์˜ [0]๋ฒˆ์งธ, [1]๋ฒˆ์งธ, [2]๋ฒˆ์งธ...[n]๋ฒˆ์งธ index์— ๊นŒ์ง€ ๊ฐ’์ด ๋“ค์–ด๊ฐ€์•ผ ํ•˜๋Š”๋ฐ...

[0]๋ฒˆ์งธ๋งŒ ๊ณ„์‚ฐํ•˜๊ณ  ๊ฐ’์„ ๋ฐ˜ํ™˜

2์ฐจ ์‹œ๋„ = ์‹คํŒจ

let birthDay = ['2019-03-21', '2019-04-21', '2019-05-21'];
let dateArr = [];

const formatDate = dates =>
  dates.map ( i => { for (let i in dates) {
    dateArr += `${dates[i].slice(0, 4)}๋…„ ${dates[i].slice(5, 7)}์›” ${dates[i].slice(8, 10)}์ผ`;
  }
  return dateArr;
})

formatDate(birthDay);

์•ž์ž๋ฆฌ ํ•œ๋ณ„๋‹˜์˜ ๋„์›€์œผ๋กœ slice ๋กœ ์ ‘๊ทผํ•˜๋Š”๊ฒŒ ํ‹€๋ ธ๋‹ค๋Š”๊ฑธ ์•Œ๊ฒŒ ๋˜์—ˆ๋‹ค.....ใ…Žใ…Ž....

๊ทธ๋Ÿฌ๋ฉด birthDay์˜ ๊ฐ [i] ๋ฒˆ์งธ ์š”์†Œ๋“ค์„ split ์œผ๋กœ "-" ๋ฅผ ๊ธฐ์ค€์œผ๋กœ ์ž๋ฅธ๋‹ค์Œ์— "๋…„","์›”","์ผ"์„ ๋ถ™์ด๋ฉด ๋  ๊ฒƒ ๊ฐ™๋‹ค!!

๋‚˜์˜ ํ•ด๊ฒฐ ๋ฐฉ๋ฒ•

let birthDay = ['2019-03-21', '2019-04-21', '2019-05-21'];
  
const formatDate = dates => {
  let format = dates.map( arr => {
    let date = arr.split('-');
    return `${date[0]}๋…„ ${date[1]}์›” ${date[2]}์ผ`; 
	 })
 return format;
}

formatDate(birthDay); // [ '2019๋…„ 03์›” 21์ผ', '2019๋…„ 04์›” 21์ผ', '2019๋…„ 05์›” 21์ผ' ]

์šฐ์™•!!!!!!!!! ํ•œ๋ณ„๋‹˜ ์ตœ๊ณ ๐Ÿ˜‚๐Ÿ˜‚๐Ÿ˜‚๐Ÿ˜‚๐Ÿ˜‚๐Ÿ˜‚๐Ÿ˜‚๐Ÿ˜‚

split ์จ์„œ "-" ๋‹จ์œ„๋กœ ์ชผ๊ฐœ ๋†“์œผ๋‹ˆ๊นŒ for ๋ฌธ ๋Œ๋ฆด ์ผ๋„ ์—†๊ณ  ๊ฐ„๋‹จํ•˜๊ฒŒ ๋๋ƒˆ๋‹ค๐Ÿ‘!!!

์—ฌ๊ธฐ์„œ ํฌ์ธํŠธ๋Š” ์ด๋ ‡๊ฒŒ ๊ณ„์‚ฐํ•œ ๊ฒฐ๊ณผ ๊ฐ’์€ let format ๋ณ€์ˆ˜์— ์ •์˜ํ•ด์„œ ๋งˆ์ง€๋ง‰์— format ๊ฐ’์€ return ํ•œ ๊ฒƒ!

model solution

const formatDate = dates => {
  return dates.map(dates => {
    let dateArr = dates.split('-');
    
    return `${dateArr[0]}๋…„ ${dateArr[1]}์›” ${dateArr[2]}์ผ`;
  });
}

๊ทธ๋ž˜์„œ ์ด๊ฒŒ ๋ญ” ๋ง?

๋‚ด๊ฐ€ ์“ด ์ฝ”๋“œ๋ž‘ ๊ฑฐ์˜ ๋น„์Šทํ•œ๋ฐ, ํฌ์ธํŠธ๋Š” ๋‚˜๋Š” dates.map ๊ฒฐ๊ณผ๊ฐ’์„ format ์ด๋ผ๋Š” ๋ณ€์ˆ˜์— ํ• ๋‹นํ•œ ๋‹ค์Œ์— ๋ณ€์ˆ˜๋ฅผ return ํ–ˆ๊ณ ....

model solution์€ dates.map ๊ฒฐ๊ณผ๊ฐ’์„ ๋ฐ”๋กœ return ํ–ˆ๋‹ค.

์ •๋ง ์ž‘์€ ์ฐจ์ด์ธ๋ฐ ๋นจ๋ฆฌ return ํ•˜๋‹ˆ๊นŒ ์ฝ”๋“œ๊ฐ€ ํ›จ์”ฌ ๊ฐ„๊ฒฐํ•˜๋‹ค.

Early Return... ํ•ญ์ƒ ๋ช…์‹ฌํ•˜์ž!!


๐Ÿ˜ฑ object (3)

๋‚˜์˜ n๋ฒˆ์งธ ํ’€์ด

const scores1 = {
  ์ƒํ™œ์†์˜ํšŒ๊ณ„: 'C',
  ๋…ผ๋ฆฌ์ ๊ธ€์“ฐ๊ธฐ: 'B',
  ๋…์ผ๋ฌธํ™”์˜์ดํ•ด: 'B+',
  ๊ธฐ์ดˆ์ˆ˜ํ•™: 'D+',
  ์˜์–ดํšŒํ™”: 'C+',
  ์ธ์ง€๋ฐœ๋‹ฌ์‹ฌ๋ฆฌํ•™: 'A+',
};
const requiredClasses1 = ['์˜์–ดํšŒํ™”', '๊ธฐ์ดˆ์ˆ˜ํ•™', '๊ณตํ•™์ˆ˜ํ•™', '์ปดํ“จํ„ฐ๊ณผํ•™๊ฐœ๋ก '];

const getExamResult = (scores, requiredClasses) => {
  const result = {};

  const numScore = {
    'A+': 4.5,
    A: 4,
    'B+': 3.5,
    B: 3,
    'C+': 2.5,
    C: 2,
    'D+': 1.5,
    D: 1,
    F: 0,
  };

  for (let i in requiredClasses) {
    let required = requiredClasses[i]; // ํ•„์ˆ˜ ๊ณผ๋ชฉ
    result[required] = 0;
  }

  for (let key in scores) {
    let subject = scores[key]; // ๊ณผ๋ชฉ๋ช…
    result[key] = subject;
  }

  for (let value in scores) {
    let score = scores[value]; // ์ˆซ์ž ์ ์ˆ˜
    result[value] = numScore[score];
  }

  return result;
};

 getExamResult(scores1, requiredClasses1);

๋Œ€๋žต ํ•˜๋ฃจ์ •๋„ ๊ฑธ๋ฆฐ object (3) ๋ฌธ์ œ....๐Ÿคช
๋Œ€์ฒด ๋ช‡๋ฒˆ์งธ ํ’€์ด์ธ์ง€...์ฒ˜์Œ ํ’€์ด๋Š” ์–ด๋• ๋Š”์ง€...๊ธฐ์–ต๋„ ์•ˆ๋‚œ๋‹ค.

(๊ทธ๋•Œ๊ทธ๋•Œ ๊ธฐ๋กํ–ˆ์–ด์•ผ ํ–ˆ๋Š”๋ฐใ… ใ…  ์ž๊พธ ๋ง‰ํžˆ๋‹ˆ ๋‹ต๋‹ตํ•ด์„œ ์ง€์šฐ๊ณ  ๋‹ค์‹œ ํ•ด๋ณด๊ณ  ์ง€์šฐ๊ณ  ๋‹ค์‹œํ•ด๋ณด๊ณ ๋ฅผ ์‰ด ์ƒˆ ์—†์ด ํ–ˆ๋˜๊ฒƒ ๊ฐ™๋‹ค....)

์ผ๋‹จ ๋ฌธ์ œ ์ดํ•ดํ•˜๋Š”๋ฐ๋งŒ 1์‹œ๊ฐ„ ์ •๋„ ๊ฑธ๋ฆฐ ๊ฒƒ ๊ฐ™๊ณ ....ใ…‹ใ…‹ใ…‹ใ…‹ใ…‹ใ…‹

object (3) ๋ฌธ์ œ ๋“ค์–ด๊ฐ€๊ธฐ ์ „์— ๋“ค์—ˆ๋˜, ์Šนํ˜„๋‹˜์˜ ๋ฏธ๋‹ˆ ์„ธ์…˜์ด ๋งŽ์ด ๋„์›€์ด ๋๋‹ค!

๐Ÿ’๐Ÿปโ€โ™€๏ธ ์ค‘์š” point

  • ๋นˆ ๊ฐ์ฒด { } ๋ฅผ ์„ ์–ธํ•˜๊ณ , ๊ทธ ๊ฐ์ฒด์— ๊ฐ’์„ ๋„ฃ๊ณ  ์‹ถ์œผ๋ฉด obj.name = "solmi"; ๋กœ ๋„ฃ์„ ์ˆ˜ ์žˆ๋‹ค.
    => ์—ฌ๊ธฐ์„œ name = key , "solmi" = value

  • key ๊ฐ’, value ๊ฐ’์€ ๋ณ€์ˆ˜๋กœ๋„ ๋„ฃ์„ ์ˆ˜ ์žˆ๋‹ค!
const obj  = {
	"A": 4, // ์ด 4๊ฐ’์ด ํ•„์š”ํ•˜๋‹ค๋ฉด,
}
const obj2 = {
	"hi": "A", // ๊ทผ๋ฐ ์ด obj2 ๋ฅผ ์ด์šฉํ•ด์•ผ ํ•œ๋‹ค๋ฉด
}
obj[obj2["hi"]]; // 4
  • const๋กœ ๋ณ€์ˆ˜๋ฅผ ์„ ์–ธํ•˜๊ณ  ๋นˆ ๊ฐ์ฒด๋ฅผ ํ• ๋‹นํ•ด๋„, ๊ฐ์ฒด ์•ˆ์— ๊ฐ’์ด ์ถ”๊ฐ€๋กœ ๋“ค์–ด์˜ฌ ์ˆ˜ ์žˆ๋‹ค.
    => const ์ธ๋ฐ ์–ด๋–ป๊ฒŒ? โ‡’ ๊ฐ์ฒด์˜ ๊ธฐ๋ณธ ์†์„ฑ ๋•Œ๋ฌธ!!
    ๊ฐ์ฒด๋Š” ๋ฉ”๋ชจ๋ฆฌ์— ์ €์žฅ๋˜๋Š”๊ฒŒ ์•„๋‹Œ, ๊ฐ ์š”์†Œ๊ฐ€ ์ €์žฅ๋œ ๋ฉ”๋ชจ๋ฆฌ๋ฅผ ๊ฐ€๋ฅดํ‚ค๋Š” reference๊ฐ€ ์ €์žฅ๋˜๊ธฐ ๋•Œ๋ฌธ์—!

  • ๋ฐฐ์—ด์„ ๊ฐ์ฒด์˜ key ๊ฐ’์œผ๋กœ ํ™œ์šฉํ•˜๊ณ  ์‹ถ๋‹ค๋ฉด?
const obj = {};
const arr = ["a", "b", "c"]; // obj ์•ˆ์— a=10, b=10, c=10 ์œผ๋กœ ๋„ฃ๊ณ  ์‹ถ๋‹ค๋ฉด?
for(let i in arr) { // for ๋ฌธ์œผ๋กœ ๋Œ๋ฉด์„œ
	obj[arr[i]] = 10; // arr[i]์— value ๊ฐ’ ์ง€์ •
}
// obj = { a: 10, b: 10, c: 10 } ์œผ๋กœ ์ถœ๋ ฅ

๐Ÿ‘‰ ๋นˆ object์— ๊ฐ’์ด ์–ด๋–ป๊ฒŒ ๋“ค์–ด๊ฐ€๋Š”์ง€, ์–ด๋–ป๊ฒŒ ๋„ฃ์„ ์ˆ˜ ์žˆ๋Š”์ง€ ๊ณ ๋ฏผํ•ด๋ณด๊ธฐ

model solution 1

const getExamResult = (scores, requiredClasses) => {
  const result = scores;
  
  for(let key in result){
    if(result[key] === "A+"){
      result[key] = 4.5;
    }
    else if(result[key] === "A"){
      result[key] = 4;
    }
    else if(result[key] === "B+"){
      result[key] = 3.5;
    }
    else if(result[key] === "B"){
      result[key] = 3;
    }
    else if(result[key] === "C+"){
      result[key] = 2.5;
    }
    else if(result[key] === "C"){
      result[key] = 2;
    }
    else if(result[key] === "D+"){
      result[key] = 1.5;
    }
    else if(result[key] === "D"){
      result[key] = 1;
    }
    else if(result[key] === "F"){
      result[key] = 0;
    }
  }
  
  requiredClasses.forEach((el) => {
   if(result[el] === undefined){
      result[el] = 0;
    }
  })
  
  return result
}

model solution 1 ์€ ๊ฐ๊ฐ์˜ key ๊ฐ’์„ ํ•™์ ๊ณผ ๋น„๊ตํ•ด์„œ, ์ผ์น˜ํ•  ๊ฒฝ์šฐ ์ˆซ์ž ํ•™์  ๊ฐ’์„ return ํ•˜๋Š” ๋ฐฉ์‹์œผ๋กœ ์ž‘์„ฑ๋˜์—ˆ๋‹ค.

if ์™€ else if ์˜ ํ˜ผ๋ˆ์˜ ๋„๊ฐ€๋‹ˆํƒ•์ด์ง€๋งŒ.... ์–ด๋ ค์šด ์กฐ๊ฑด์ด ์•„๋‹ˆ๊ณ  ์˜์–ด ํ•™์ -์ˆซ์ž ํ•™์ ์ด ์„œ๋กœ link ๋˜์–ด ์žˆ์œผ๋‹ˆ๊นŒ, ์˜คํžˆ๋ ค ์‚ฌ๋žŒ ์ž…์žฅ์—์„œ ๋ดค์„๋•Œ ๋” ๋ˆˆ์— ์ž˜๋“ค์–ด์˜ค๋Š”๊ฒƒ ๊ฐ™๊ธฐ๋„ ํ•จ!

model solution 2

const getExamResult = (scores, requiredClasses) => {
  const result = {}
  const list = {
    "A+":4.5,
    "A":4,
    "B+":3.5,
    "B":3,
    "C+":2.5,
    "C":2,
    "D+":1.5,
    "D":1,
    "F":0    
  }
  
  for(let key in scores){
    result[key] = list[scores[key]];
  }
  
  const keys=Object.keys(result)
  for(let i in requiredClasses){
    if(!keys.includes(requiredClasses[i])){
      result[requiredClasses[i]] = 0;
    }
  }
  
  return result;
}

model solution 2 ๋Š” ๋ฐ˜๋ฉด์— ์™•์™•๊ฐ„๋‹จ... Object.keys ์™€ includes ๋ฉ”์„œ๋“œ๋ฅผ ์ด์šฉํ•ด์„œ ์™„์ „ ์งง๊ณ  ๊ฐ„๊ฒฐํ•˜๊ฒŒ ์ž‘์„ฑ๋˜์—ˆ๋‹น.....

๋‚˜๋Š” for in ๊ตฌ๋ฌธ 3๋ฒˆ ๋ฐ˜๋ณตํ–ˆ๋Š”๋Ž…......( ์‹ฌ์ง€์–ด ๊ทธ๊ฒƒ๋„ ํ•˜๋ฃจ ๊ฑธ๋ฆผ๐Ÿคฎ)


์šฐ์™•๐Ÿ˜ญ ๋“œ๋””์–ด JavaScript Repl.it ๋!!!!!
๋„์›€์ฃผ๋Š” ๋ฉ˜ํ† ๋‹˜, ๋™๊ธฐ๋‹˜ ๋ชจ๋‘๋ชจ๋‘ ๊ฐ์‚ฌํ•ด์˜คใ… ใ… ใ… ใ… ใ… 

profile
ํ•˜๋ฃจ๋Š” ์น˜์—ดํ•˜๊ฒŒ ์ธ์ƒ์€ ์—ฌ์œ ๋กญ๊ฒŒ

0๊ฐœ์˜ ๋Œ“๊ธ€