항해 프로그래머스
항해3주차 자바스크립트 프로그래머스 - 두 수의 나눗셈/Math.@@
완두노예
2023. 1. 25. 09:31
const solution = (num1, num2) => (num1 / num2 * 1000) << 0
처음 나의 풀이 = X
function solution(num1, num2) {
var answer = int(num1/num2)*1000 ;
return answer;
}
두번째 풀이 = X--------> Int 왜 안되는걸까??
function solution(num1, num2) {
var answer = int(num1/num2*1000);
return answer;
}
다시 수정 풀이 = O
function solution(num1, num2) {
var answer = num1/num2*1000;
return Math.trunc(answer);
}
Math.ceil( ) | 소수값이 존재할 때 값을 올리는 역할을 하는 함수 ex) 3.5 → 4 , -3.5 → -3 |
Math.floor( ) | 소수값이 존재할 때 바닥까지 내린다고 생각 ex) 3.5 → 3 , -3.5 → -4 |
Math.round( ) | 소수값에 따라 올리거나 버리는 역할을 하는 반올림 함수 |
Math.trunc( ) | 소수점 이하는 버린다 |
<floor 와 trunc의 차이점>은 floor는 숫자를 끌어내리는 느낌인데, trunc는 단순하게 소수점 이하만 삭제시키고 끝이다
Math.ceil( ) : https://developer.mozilla.org/ko/docs/Web/JavaScript/Reference/Global_Objects/Math/ceil
Math.floor( ) : https://developer.mozilla.org/ko/docs/Web/JavaScript/Reference/Global_Objects/Math/floor
Math.round( ) : https://developer.mozilla.org/ko/docs/Web/JavaScript/Reference/Global_Objects/Math/round
Math.trunc( ) : https://developer.mozilla.org/ko/docs/Web/JavaScript/Reference/Global_Objects/Math/trunc
다른 사람의 풀이
const solution = (num1, num2) => Math.floor(num1 / num2 * 1000)
function solution(num1, num2) {
return Math.trunc(num1 / num2 * 1000);
}
function solution(num1, num2) {
return num1*1000/num2<<0;
}
ParseInt -------> 알아보자!
function solution(num1, num2) {
return parseInt(num1 / num2 * 1000);
}
function solution(num1, num2) {
return ~~(num1/num2*1000);
}
function solution(num1, num2) {
if (num2 === 0) {
pass
} else {
return Math.floor((num1 / num2) * 1000)
}
}
function solution(num1, num2) {
const answer = num1/num2*1000;
return ~~answer;
}