๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ

ํ”„์—” ๊ณต๋ถ€/๐Ÿซง ์•Œ๊ณ ๋ฆฌ์ฆ˜ ๊ณต๋ถ€

[ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค/Javascript] [level.0] ๋ฐฐ์—ด ํšŒ์ „์‹œํ‚ค || ์ฝ”๋”ฉํ…Œ์ŠคํŠธ ์ž…๋ฌธ

728x90
function solution(numbers, direction) {
    var answer = [];

    if ("right" == direction) {
        numbers.unshift(numbers.pop());
    } else {
        numbers.push(numbers.shift());
    }

    answer = numbers;

    return answer;
}

์ œ์ผ ๋ฒ ์ŠคํŠธ๋ผ๊ณ  ์ƒ๊ฐํ•˜๋Š” ์ฝ”๋“œ.

 

 

Array.prototype.pop()

 

https://developer.mozilla.org/ko/docs/Web/JavaScript/Reference/Global_Objects/Array/pop

 

Array.prototype.pop() - JavaScript | MDN

pop() ๋ฉ”์„œ๋“œ๋Š” ๋ฐฐ์—ด์—์„œ ๋งˆ์ง€๋ง‰ ์š”์†Œ๋ฅผ ์ œ๊ฑฐํ•˜๊ณ  ๊ทธ ์š”์†Œ๋ฅผ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค.

developer.mozilla.org

Array.pop() ์„ ์‚ฌ์šฉํ•˜๋ฉด ๋ณ€ํ™”ํ•œ Array์™€ ์—†์•ค ๋งˆ์ง€๋ง‰ ๊ฐ’ ๋‘˜ ๋‹ค ์–ป์„ ์ˆ˜ ์žˆ๋‹ค.

const arr = [๐Ÿ‡,๐Ÿˆ,๐Ÿ‰,๐ŸŠ];

arr.pop() ์„ ํ•˜๊ฒŒ ๋˜๋ฉด

arr = [ ๐Ÿ‡,๐Ÿˆ,๐Ÿ‰] ์ด๊ณ , 

arr.pop()๊ฐ€ ๋ฐ˜ํ™˜ํ•˜๋Š” ๊ฐ’์€ ๐ŸŠ์ด๋‹ค.

 

Array.prototype.unshift()

unshift() ๋ฉ”์„œ๋“œ๋Š” ์ƒˆ๋กœ์šด ์š”์†Œ๋ฅผ ๋ฐฐ์—ด์˜ ๋งจ ์•ž์ชฝ์— ์ถ”๊ฐ€ํ•˜๊ณ , ์ƒˆ๋กœ์šด ๊ธธ์ด๋ฅผ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค.

(push๋ฅผ ํ•˜๋ฉด ๋’ค์—์„œ๋ถ€ํ„ฐ ๊ฐ’์„ ๋„ฃ์–ด์ฃผ๊ธฐ ๋•Œ๋ฌธ์— push์˜ ๋ฐ˜๋Œ€๋ผ๊ณ  ์ƒ๊ฐํ•˜๋ฉด ๋œ๋‹ค.)

https://developer.mozilla.org/ko/docs/Web/JavaScript/Reference/Global_Objects/Array/unshift

 

Array.prototype.unshift() - JavaScript | MDN

unshift() ๋ฉ”์„œ๋“œ๋Š” ์ƒˆ๋กœ์šด ์š”์†Œ๋ฅผ ๋ฐฐ์—ด์˜ ๋งจ ์•ž์ชฝ์— ์ถ”๊ฐ€ํ•˜๊ณ , ์ƒˆ๋กœ์šด ๊ธธ์ด๋ฅผ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค.

developer.mozilla.org

arr = [ ๐Ÿ‡,๐Ÿˆ,๐Ÿ‰] ๋ฅผ ๋‹ค์‹œ ์˜ˆ๋กœ ๋“ค์ž๋ฉด, 

arr.unshift(๐Ÿ) ์„ ํ•˜๋ฉด, 

arr = [ ๐Ÿ,๐Ÿ‡,๐Ÿˆ,๐Ÿ‰]  ์ด๋ ‡๊ฒŒ ๋งจ ์•ž์— ๋„ฃ์–ด์ค€๋‹ค.

 

 

 

Array.prototype.shift()

shift() ๋ฉ”์„œ๋“œ๋Š” ๋ฐฐ์—ด์—์„œ ์ฒซ ๋ฒˆ์งธ ์š”์†Œ๋ฅผ ์ œ๊ฑฐํ•˜๊ณ , ์ œ๊ฑฐ๋œ ์š”์†Œ๋ฅผ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค. ์ด ๋ฉ”์„œ๋“œ๋Š” ๋ฐฐ์—ด์˜ ๊ธธ์ด๋ฅผ ๋ณ€ํ•˜๊ฒŒ ํ•ฉ๋‹ˆ๋‹ค.

(pop ์ด๋ž‘ ๋ฐ˜๋Œ€๋ผ๊ณ  ์ƒ๊ฐํ•˜๋ฉด ๋œ๋‹ค.)

https://developer.mozilla.org/ko/docs/Web/JavaScript/Reference/Global_Objects/Array/shift

 

Array.prototype.shift() - JavaScript | MDN

shift() ๋ฉ”์„œ๋“œ๋Š” ๋ฐฐ์—ด์—์„œ ์ฒซ ๋ฒˆ์งธ ์š”์†Œ๋ฅผ ์ œ๊ฑฐํ•˜๊ณ , ์ œ๊ฑฐ๋œ ์š”์†Œ๋ฅผ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค. ์ด ๋ฉ”์„œ๋“œ๋Š” ๋ฐฐ์—ด์˜ ๊ธธ์ด๋ฅผ ๋ณ€ํ•˜๊ฒŒ ํ•ฉ๋‹ˆ๋‹ค.

developer.mozilla.org

 

const arr = [๐Ÿ‡,๐Ÿˆ,๐Ÿ‰,๐ŸŠ];

arr.shift() ์„ ํ•˜๊ฒŒ ๋˜๋ฉด

arr = [ ๐Ÿˆ,๐Ÿ‰,๐ŸŠ] ์ด๊ณ , 

arr.pop()๊ฐ€ ๋ฐ˜ํ™˜ํ•˜๋Š” ๊ฐ’์€ ๐Ÿ‡์ด๋‹ค.

 

 

 

 

 

 

function solution(numbers, direction) {
    let resultArr = [];
    for (let i = 0; i < numbers.length; i++) {
        if (direction === "right") {
            i + 1 !== numbers.length ? resultArr[i + 1] = numbers[i] : resultArr[0] = numbers[i];
        } else if (direction === "left") {
            i - 1 < 0 ? resultArr[numbers.length - 1] = numbers[i] : resultArr[i - 1] = numbers[i];
        }
    }
    console.log(resultArr);
    return resultArr;
}

์ด๊ฑด ๋‚˜์˜ ์ฝ”๋“œ! ๋ฒ”์œ„๋ฅผ ๋ฒ—์–ด๋‚˜๋Š” index๋ฅผ ์ œ์ผ ๋์œผ๋กœ ๋ณด๋‚ด์ฃผ๋Š” ๋ฐฉ์‹์œผ๋กœ ์ฝ”๋“œ๋ฅผ ์งฐ๋Š”๋ฐ, ์œ„์˜ pop๊ณผ shift ํ•จ์ˆ˜๋ฅผ ์•Œ์•˜๋‹ค๋ฉด ํ›จ์”ฌ ๊ฐ„๋‹จํ•˜๊ฒŒ ์ž‘์„ฑ์ด ๊ฐ€๋Šฅํ–ˆ๋‹ค.

728x90
๋ฐ˜์‘ํ˜•