๐Ÿ’ก Level 1 | ๋‚ด์  (JavaScript)

rimmzยท2022๋…„ 5์›” 5์ผ
0
post-thumbnail

๐Ÿ“Œ ๋ฌธ์ œ

  • ๊ธธ์ด๊ฐ€ ๊ฐ™์€ ๋‘ 1์ฐจ์› ์ •์ˆ˜ ๋ฐฐ์—ด a, b๊ฐ€ ๋งค๊ฐœ๋ณ€์ˆ˜๋กœ ์ฃผ์–ด์ง‘๋‹ˆ๋‹ค. a์™€ b์˜ ๋‚ด์ ์„ return ํ•˜๋„๋ก solution ํ•จ์ˆ˜๋ฅผ ์™„์„ฑํ•ด์ฃผ์„ธ์š”.
    ์ด๋•Œ, a์™€ b์˜ ๋‚ด์ ์€ a[0]*b[0] + a[1]*b[1] + ... + a[n-1]*b[n-1] ์ž…๋‹ˆ๋‹ค. (n์€ a, b์˜ ๊ธธ์ด)

    https://programmers.co.kr/learn/courses/30/lessons/70128

โœจ ๋ฌธ์ œ ํ’€์ด

function solution(a, b) {
    let result = 0;
    
    for (let i in a) {
        result += a[i] * b[i]
    }
    
    return result
}

๐Ÿ’ฌ ์ •๋ฆฌ

for..in : ํ•ด๋‹น ๊ฐ์ฒด์˜ ๋ชจ๋“  ์—ด๊ฒจํ•  ์ˆ˜ ์žˆ๋Š” ํ”„๋กœํผํ‹ฐ๋ฅผ ์ˆœํšŒ

  • ๋ฐฐ์—ด ์š”์†Œ ์ ‘๊ทผ ์˜ˆ์ œ
const array = { 1, 2, 3 };

for (const i in array) {
  console.log(i);
}

// expected output:
// 1
// 2
// 3
  • ๊ฐ์ฒด ํ”„๋กœํผํ‹ฐ ์ ‘๊ทผ ์˜ˆ์ œ
const object = { a: 1, b: 2, c: 3 };

for (const property in object) {
  console.log(`${property}: ${object[property]}`);
}

// expected output:
// "a: 1"
// "b: 2"
// "c: 3"
profile
#์˜์š•๋„˜์น˜๋Š”#๐Ÿ’ป#โœจ#Front-end#๐Ÿ’ช๐Ÿป

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