ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • [JS 100์ œ] ๋ฌธ์ œ18 - ํ‰๊ท ์ ์ˆ˜
    ๐Ÿ›  develop/CodingTest 2022. 11. 15. 09:43

    ๋ฌธ์ œ

    ์˜ํ•˜๋„ค ๋ฐ˜์€ ๊ตญ์–ด, ์ˆ˜ํ•™, ์˜์–ด ์‹œํ—˜์„ ๋ณด์•˜์Šต๋‹ˆ๋‹ค. ์˜ํ•˜๋Š” ์นœ๊ตฌ๋“ค์˜ ํ‰๊ท  ์ ์ˆ˜๋ฅผ ๊ตฌํ•ด์ฃผ๊ธฐ๋กœ ํ–ˆ์Šต๋‹ˆ๋‹ค. ๊ณต๋ฐฑ์œผ๋กœ ๊ตฌ๋ถ„ํ•˜์—ฌ ์„ธ ๊ณผ๋ชฉ์˜ ์ ์ˆ˜๊ฐ€ ์ฃผ์–ด์ง€๋ฉด ์ „์ฒด ํ‰๊ท  ์ ์ˆ˜๋ฅผ ๊ตฌํ•˜๋Š” ํ”„๋กœ๊ทธ๋žจ์„ ์ž‘์„ฑํ•˜์„ธ์š”. ๋‹จ, ์†Œ์ˆซ์  ์ž๋ฆฌ๋Š” ๋ชจ๋‘ ๋ฒ„๋ฆฝ๋‹ˆ๋‹ค.

    ์ž…์ถœ๋ ฅ
    
    ์ž…๋ ฅ : 20 30 40
    ์ถœ๋ ฅ : 30

     

    ๋‚˜์˜ ํ’€์ด

    let record = prompt("๊ตญ์–ด ์ˆ˜ํ•™ ์˜์–ด ์ ์ˆ˜๋ฅผ ๊ณต๋ฐฑ ๊ธฐ์ค€์œผ๋กœ ์ฐจ๋ก€๋Œ€๋กœ ์ž…๋ ฅํ•˜์‹œ์˜ค").split(" "); // ๊ณต๋ฐฑ ๊ธฐ์ค€์œผ๋กœ ์ž˜๋ผ์„œ ๋‹ค์‹œ ๋ฐฐ์—ด๋กœ
    let result = 0;
    
    for (let i = 0; i < record.length; i++) {
      result += Number(record[i]);
      result /= record.length - 1;
    }
    
    console.log(Math.floor(result)); // ์†Œ์ˆ˜์  ์ฒ˜๋ฆฌ
    1. ์ด ์ ์ˆ˜๋ฅผ ๊ณต๋ฐฑ์œผ๋กœ ๋ฐ›๋˜, split ํ•จ์ˆ˜๋ฅผ ์ด์šฉํ•˜์—ฌ ๊ณต๋ฐฑ์œผ๋กœ ์ž˜๋ผ ๋ฐฐ์—ด๋กœ ๋ณ€์ˆ˜์— ์ €์žฅํ•ด์ค€๋‹ค.
    2. for ๋ฌธ์„ ์ด์šฉํ•˜์—ฌ record ๊ธธ์ด ๋ฏธ๋งŒ๊นŒ์ง€ ๋Œ๋ ค์ค€๋‹ค. 
    3. for ๋ฌธ ๋‚ด๋ถ€์—์„œ record ์˜ ๊ฐ ๋ฐฐ์—ด ํ•˜๋‚˜์”ฉ ๊ฐ’์„ ๋”ํ•ด์ฃผ๊ณ , ๋“ค์–ด์˜จ ๊ฐ’ -1 ๋งŒํผ ๋‚˜๋ˆ ์ค€๋‹ค.
    4. ๋งˆ์ง€๋ง‰ ๊ฒฐ๊ณผ๊ฐ’์„ ๋‚ผ ๋•Œ, ์†Œ์ˆ˜์ ์„ ๋ฒ„๋ฆฌ๊ธฐ ์œ„ํ•ด ํ•จ์ˆ˜ Math.floor()๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ์ถœ๋ ฅํ•ด์ค€๋‹ค.

     

    ์ •๋‹ต๊ณผ ๋ฐ˜์„ฑ์˜ ์‹œ๊ฐ„

    const scores = prompt('์„ธ ๊ณผ๋ชฉ์˜ ์ ์ˆ˜๋ฅผ ์ž…๋ ฅํ•˜์„ธ์š”.').split(' ');
    let sum = 0;
    
    for (let i=0; i<3; i++){
      sum += parseInt(scores[i], 10); // ์‹ญ์ง„์ˆ˜์˜ ํ˜•ํƒœ์˜ ์ˆซ์ž๋กœ ๋ฐ์ดํ„ฐ ํƒ€์ž…์„ ๋ณ€ํ™˜ํ•ฉ๋‹ˆ๋‹ค.
    }
    
    console.log(Math.floor(sum/3)); //Math.floor ๋ฉ”์„œ๋“œ๋Š” ์†Œ์ˆ˜์  ์ž๋ฆฌ๋ฅผ ๋ชจ๋‘ ๋ฒ„๋ฆผํ•ฉ๋‹ˆ๋‹ค.
    • result๋ฅผ ํ‰๊ท  ๋‚ด์ฃผ๋Š” ๋ถ€๋ถ„์„ for ๋ฌธ ๋ฐ”๊นฅ์— ๋‘์—ˆ์–ด์•ผ ํ•œ๋‹ค.. ๋ฌผ๋ก  ๊ฒฐ๊ณผ๊ฐ’์€ ๋™์ผํ•  ์ˆ˜ ์žˆ๊ฒ ์œผ๋‚˜ ํ˜น์‹œ ๋ชจ๋ฅผ ์˜ค์ฐจ๊ฐ’์ด ์ƒ๊ธธ ์ˆ˜๋„ ์žˆ๋‹ค.
    • ๋˜ํ•œ parseInt()๋ฅผ ์ƒˆ๋กœ ์•Œ๊ฒŒ ๋˜์—ˆ๋‹ค.
    • ์žฌํ’€์ด ํ•œ ๊ฒฐ๊ณผ๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™๋‹ค.

     

    let record = prompt("๊ตญ์–ด ์ˆ˜ํ•™ ์˜์–ด ์ ์ˆ˜๋ฅผ ๊ณต๋ฐฑ ๊ธฐ์ค€์œผ๋กœ ์ฐจ๋ก€๋Œ€๋กœ ์ž…๋ ฅํ•˜์‹œ์˜ค").split(" ");
    let result = 0;
    
    for (let i = 0; i < record.length; i++) {
      result += parseInt(record[i], 10);
    }
    
    console.log(Math.floor(result / record.length - 1));

     

    JS ๊ฐ„๋‹จ ๋ณต์Šต

    Number๊ณผ parseInt ์˜ ์ฐจ์ด

     

    ์•„๋ž˜์˜ ํฌ์ŠคํŒ…์œผ๋กœ ๋ณต์Šตํ–ˆ์Šต๋‹ˆ๋‹ค!

    https://m-ur-phy.tistory.com/entry/Number-%EA%B3%BC-parseInt-%EC%9D%98-%EC%B0%A8%EC%9D%B4%EB%8A%94-%EB%AC%B4%EC%97%87%EC%9D%BC%EA%B9%8C

    ๋Œ“๊ธ€