๋ฌธ์ œ

ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค ๋ชจ๋ฐ”์ผ์€ ๊ฐœ์ธ์ •๋ณด ๋ณดํ˜ธ๋ฅผ ์œ„ํ•ด ๊ณ ์ง€์„œ๋ฅผ ๋ณด๋‚ผ ๋•Œ ๊ณ ๊ฐ๋“ค์˜ ์ „ํ™”๋ฒˆํ˜ธ์˜ ์ผ๋ถ€๋ฅผ ๊ฐ€๋ฆฝ๋‹ˆ๋‹ค.
์ „ํ™”๋ฒˆํ˜ธ๊ฐ€ ๋ฌธ์ž์—ด phone_number๋กœ ์ฃผ์–ด์กŒ์„ ๋•Œ, ์ „ํ™”๋ฒˆํ˜ธ์˜ ๋’ท 4์ž๋ฆฌ๋ฅผ ์ œ์™ธํ•œ ๋‚˜๋จธ์ง€ ์ˆซ์ž๋ฅผ ์ „๋ถ€ *์œผ๋กœ ๊ฐ€๋ฆฐ ๋ฌธ์ž์—ด์„ ๋ฆฌํ„ดํ•˜๋Š” ํ•จ์ˆ˜, solution์„ ์™„์„ฑํ•ด์ฃผ์„ธ์š”.

ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค ์ฝ”๋”ฉํ…Œ์ŠคํŠธ

 

์‚ฌ์šฉํ•œ ํ•จ์ˆ˜ 

substring() : ์‹œ์ž‘ ์ธ๋ฑ์Šค์™€ ์ข…๋ฃŒ ์ธ๋ฑ์Šค์˜ ๋ฌธ์ž์—ด์„ ๋ฐ˜ํ™˜ํ•œ๋‹ค.

let phone_number = 01023456789
hide = phone_number.substring(phone_number.length-4,0)  // 0102345
show = phone_number.substring(phone_number.length-4,phone_number.length) // 6789

 

ํ•ด๊ฒฐ

 

function solution(phone_number) {
    var answer = '';
    star = '*'
    hide = phone_number.substring(phone_number.length-4,0).length
    show = phone_number.substring(phone_number.length-4,phone_number.length);
    answer = star.repeat(hide) + show

    return answer;
}

๋ฌธ์ œ
์ •์ˆ˜๋ฅผ ๋‹ด๊ณ  ์žˆ๋Š” ๋ฐฐ์—ด arr์˜ ํ‰๊ท ๊ฐ’์„ returnํ•˜๋Š” ํ•จ์ˆ˜, solution์„ ์™„์„ฑํ•ด๋ณด์„ธ์š”.

 

ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค ์ฝ”๋”ฉํ…Œ์ŠคํŠธ

์‚ฌ์šฉํ•œ ํ•จ์ˆ˜

reduce()์˜ ์ดํ•ฉ ๊ตฌํ•˜๋Š” ํ•จ์ˆ˜๋ฅผ ์ฐธ๊ณ ํ•˜์˜€๋‹ค.

var sum = [0, 1, 2, 3].reduce(function (accumulator, currentValue) {
  return accumulator + currentValue;
}, 0);
var total = [ 0, 1, 2, 3 ].reduce(
  ( accumulator, currentValue ) => accumulator + currentValue,
  0
);

 

ํ•ด๊ฒฐ

 

function solution(arr) {
    var answer = 0; 
    answer = arr.reduce((a,b) => (a+b))/arr.length ;

    return answer;
}

 

 

 

 

 

๋ฌธ์ œ

0๋ถ€ํ„ฐ 9๊นŒ์ง€์˜ ์ˆซ์ž ์ค‘ ์ผ๋ถ€๊ฐ€ ๋“ค์–ด์žˆ๋Š” ์ •์ˆ˜ ๋ฐฐ์—ด 'numbers'๊ฐ€ ๋งค๊ฐœ๋ณ€์ˆ˜๋กœ ์ฃผ์–ด์ง‘๋‹ˆ๋‹ค. 
'numbers' ์—์„œ ์ฐพ์„ ์ˆ˜ ์—†๋Š” 0๋ถ€ํ„ฐ 9๊นŒ์ง€์˜ ์ˆซ์ž๋ฅผ ๋ชจ๋‘ ์ฐพ์•„ ๋”ํ•œ ์ˆ˜๋ฅผ return ํ•˜๋„๋ก solution ํ•จ์ˆ˜๋ฅผ ์™„์„ฑํ•ด์ฃผ์„ธ์š”.

ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค ์ฝ”๋”ฉํ…Œ์ŠคํŠธ

 

์‚ฌ์šฉํ•œ ํ•จ์ˆ˜ 

includes() : ๊ด„ํ˜ธ ๋‚ด์šฉ์„ ํฌํ•จํ•œ ๊ฐ’์„ ๋‚˜ํƒ€๋‚ธ๋‹ค. 

 

ํ•ด๊ฒฐ.

function solution(numbers) {
    var answer = 0;
    for (let i = 0; i < 10; i++) {
        if (numbers.includes(i) === true) {
            continue
        } else {       
            answer += i        
        }         
    }
    return answer;
}

 

 

 

 

ํ•ด๊ฒฐ. (๋‹ค๋ฅธ์‚ฌ๋žŒ์˜ ํ’€์ด ์ฐธ๊ณ )

function solution(numbers) {
    return 45 - numbers.reduce((cur, acc) => cur + acc, 0);
}

์‚ฌ์šฉํ•จ์ˆ˜: reduce() 

45 (1~9์˜ ์ดํ•ฉ)  -  'number'๋ณ€์ˆ˜ ๋‚ด ์ˆซ์ž ์ดํ•ฉ์„ ๋บ€ ๊ฐ’์„ ๊ตฌํ–ˆ๋‹ค. 

 

 

ํ•ด๊ฒฐ. (๋‹ค๋ฅธ์‚ฌ๋žŒ์˜ ํ’€์ด ์ฐธ๊ณ )

function solution(numbers) {
    let answer = 0;

    for(let i = 0; i <= 9; i++) {
        if(!numbers.includes(i)) answer += i;
    }

    return answer;
}

๋‚ด๊ฐ€ ์ œ์ถœํ•œ ํ’€์ด์™€ ๋น„์Šทํ•˜์ง€๋งŒ, 

ํ‘œํ˜„๋ฐฉ์‹์ด ์งง์•„์„œ ์ฐธ๊ณ ํ•˜๋ฉด ์ข‹์„ ๊ฒƒ๊ฐ™๋‹ค.

+ Recent posts