본문 바로가기
Algorithm

[JavaScript] 프로그래머스 - 몫 구하기

by 랩린안 2022. 10. 22.
// 문제 : 정수 num1, num2가 매개변수로 주어질 때, num1을 num2로 나눈 몫을 return 하도록 solution 함수를 완성해주세요.
// 반성 : Math.floor()와 parseInt()만 알고있었는데 다른 방법들을 보고 너무 놀랐음.

double tilde(~~)와

<<  shift연산자

function solution(num1, num2) {
  return parseInt(num1 / num2);
}

function solution(num1, num2) {
  var answer = num1 / num2;
  return Math.floor(answer);
}

function solution(num1, num2) {
  return (num1 / num2) << 0;
}

function solution(num1, num2) {
  return Math.trunc(num1 / num2);
}

function solution(num1, num2) {
  return ~~(num1 / num2);
}

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Left_shift

 

Left shift (<<) - JavaScript | MDN

The left shift operator (<<) shifts the first operand the specified number of bits, modulo 32, to the left. Excess bits shifted off to the left are discarded. Zero bits are shifted in from the right.

developer.mozilla.org