Jieunny์˜ ๋ธ”๋กœ๊ทธ

[JS] ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค - ์ถ”์–ต ์ ์ˆ˜ ๋ณธ๋ฌธ

Study/Coding Test

[JS] ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค - ์ถ”์–ต ์ ์ˆ˜

Jieunny 2023. 4. 4. 13:36

๐Ÿ“Œ  ๋ฌธ์ œ

์‚ฌ์ง„๋“ค์„ ๋ณด๋ฉฐ ์ถ”์–ต์— ์ –์–ด ์žˆ๋˜ ๋ฃจ๋Š” ์‚ฌ์ง„๋ณ„๋กœ ์ถ”์–ต ์ ์ˆ˜๋ฅผ ๋งค๊ธธ๋ ค๊ณ  ํ•ฉ๋‹ˆ๋‹ค. ์‚ฌ์ง„ ์†์— ๋‚˜์˜ค๋Š” ์ธ๋ฌผ์˜ ๊ทธ๋ฆฌ์›€ ์ ์ˆ˜๋ฅผ ๋ชจ๋‘ ํ•ฉ์‚ฐํ•œ ๊ฐ’์ด ํ•ด๋‹น ์‚ฌ์ง„์˜ ์ถ”์–ต ์ ์ˆ˜๊ฐ€ ๋ฉ๋‹ˆ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด ์‚ฌ์ง„ ์† ์ธ๋ฌผ์˜ ์ด๋ฆ„์ด ["may", "kein", "kain"]์ด๊ณ  ๊ฐ ์ธ๋ฌผ์˜ ๊ทธ๋ฆฌ์›€ ์ ์ˆ˜๊ฐ€ [5์ , 10์ , 1์ ]์ผ ๋•Œ ํ•ด๋‹น ์‚ฌ์ง„์˜ ์ถ”์–ต ์ ์ˆ˜๋Š” 16(5 + 10 + 1)์ ์ด ๋ฉ๋‹ˆ๋‹ค. ๋‹ค๋ฅธ ์‚ฌ์ง„ ์† ์ธ๋ฌผ์˜ ์ด๋ฆ„์ด ["kali", "mari", "don", "tony"]์ด๊ณ  ["kali", "mari", "don"]์˜ ๊ทธ๋ฆฌ์›€ ์ ์ˆ˜๊ฐ€ ๊ฐ๊ฐ [11์ , 1์ , 55์ ]]์ด๊ณ , "tony"๋Š” ๊ทธ๋ฆฌ์›€ ์ ์ˆ˜๊ฐ€ ์—†์„ ๋•Œ, ์ด ์‚ฌ์ง„์˜ ์ถ”์–ต ์ ์ˆ˜๋Š” 3๋ช…์˜ ๊ทธ๋ฆฌ์›€ ์ ์ˆ˜๋ฅผ ํ•ฉํ•œ 67(11 + 1 + 55)์ ์ž…๋‹ˆ๋‹ค.

๊ทธ๋ฆฌ์›Œํ•˜๋Š” ์‚ฌ๋žŒ์˜ ์ด๋ฆ„์„ ๋‹ด์€ ๋ฌธ์ž์—ด ๋ฐฐ์—ด name, ๊ฐ ์‚ฌ๋žŒ๋ณ„ ๊ทธ๋ฆฌ์›€ ์ ์ˆ˜๋ฅผ ๋‹ด์€ ์ •์ˆ˜ ๋ฐฐ์—ด yearning, ๊ฐ ์‚ฌ์ง„์— ์ฐํžŒ ์ธ๋ฌผ์˜ ์ด๋ฆ„์„ ๋‹ด์€ ์ด์ฐจ์› ๋ฌธ์ž์—ด ๋ฐฐ์—ด photo๊ฐ€ ๋งค๊ฐœ๋ณ€์ˆ˜๋กœ ์ฃผ์–ด์งˆ ๋•Œ, ์‚ฌ์ง„๋“ค์˜ ์ถ”์–ต ์ ์ˆ˜๋ฅผ photo์— ์ฃผ์–ด์ง„ ์ˆœ์„œ๋Œ€๋กœ ๋ฐฐ์—ด์— ๋‹ด์•„ returnํ•˜๋Š” solution ํ•จ์ˆ˜๋ฅผ ์™„์„ฑํ•ด์ฃผ์„ธ์š”.

 

๐Ÿ’ก  ์•„์ด๋””์–ด

์ด๋ฆ„ ๋ฐฐ์—ด ์•ˆ์— ๋‚ด๊ฐ€ ์ฐพ๋Š” ์‚ฌ๋žŒ์ด ์žˆ์œผ๋ฉด indexOf๋กœ ์ธ๋ฑ์Šค๋ฅผ ์ฐพ์•„์„œ yearning ๋ฐฐ์—ด์—์„œ ๊ทธ๋ฆฌ์›€ ์ ์ˆ˜๋ฅผ ์ฐพ์•„์„œ score์— ๋”ํ•ด์ค€๋‹ค.

์—†์œผ๋ฉด 0์„ ๋”ํ•ด์ค€๋‹ค.

 

โœ๏ธ  ํ’€์ด

function solution(name, yearning, photo) {
  var answer = [];

  for(let i=0; i<photo.length; i++){
    let score = 0;
    for(let j=0; j<photo[i].length; j++){
      score += name.indexOf(photo[i][j]) !== -1 ? yearning[name.indexOf(photo[i][j])] : 0;
      // ์ฐพ๋Š” ์ด๋ฆ„์ด ์žˆ๋‹ค๋ฉด ์ ์ˆ˜ ๋ฆฌํ„ด, ์—†์œผ๋ฉด 0 ๋ฆฌํ„ดํ•ด์„œ ์ ์ˆ˜์— ๋”ํ•ด์ฃผ๊ธฐ
    }
    answer.push(score);
  }
  return answer;
}