๐Ÿ’Ž

ARR PS - OX quiz

notion image
ย 
notion image
ย 

ํ’€์–ด๋ณด๊ธฐ

nums ๋ฐฐ์—ด์„ for๋ฌธ์œผ๋กœ ํƒ์ƒ‰ํ•˜๋ฉด์„œ ์›์†Œ๊ฐ€ 1์ด ๋“ค์–ด์žˆ์œผ๋ฉด 0์œผ๋กœ ์ดˆ๊ธฐํ™”๋œ ์ ์ˆ˜๋ฅผ 1์”ฉ ์ฆ๊ฐ€์‹œ์ผœ์„œ ๊ทธ ์›์†Œ๋ฅผ ๊ต์ฒด์‹œ์ผœ์ค€๋‹ค.
์ด๋ ‡๊ฒŒ ํ•˜๋ฉด ์ฒซ 1์„ ๋งŒ๋‚˜๋ฉด 1๋กœ ๋ฐ”๊ฟ”์ฃผ๊ณ  ๋‘๋ฒˆ์งธ 1์—์„œ ๋‹ค์‹œ ์ ์ˆ˜๋ฅผ ํ•˜๋‚˜ ์ฆ๊ฐ€์‹œ์ผœ์„œ ๊ต์ฒดํ•ด์„œ 2๋กœ ๋ฐ”๊พธ์–ด์ค€๋‹ค.
์—ฐ์†์ ์œผ๋กœ ํ™•์ธํ•˜๋‹ค๊ฐ€ 1์ด ์•„๋‹ ๋•Œ ๋ฐ”๋กœ ์ ์ˆ˜๋ฅผ ์ดˆ๊ธฐํ™”์‹œ์ผœ์ฃผ๊ณ  ์•„๋ฌด ์ž‘์—…๋„ ํ•˜์ง€ ์•Š๋Š”๋‹ค.
ย 
function answer(nums) {
    let score = 0;
    for (let i = 0; i < nums.length; i++) {
        if (nums[i]) {
            score++;
            nums[i] = score;
        } else score = 0;
    }
    return nums.reduce((cur, acc) => cur + acc);
}
ย 
ย 
ย