본문 바로가기

Study/Today I Learn

[TIL] 2019.04.05 - Algorithm

~ 연산자 (물결)

알고리즘을 풀다가 다른 사람이 javascript 에서 저런 문법을 사용해서 알아보았다.
~ 연산자는 비트연산자의 Bitwise NOT 으로 연산은 다음과 같다

~n == -(n+1)
~1 == -(1+1) == -2
-~a == -(-(a+1)) == a+1

Math.sqrt(x)

x의 제곱근을 반환, 음수면 NaN을 반환

Math.sqrt(9); // 3
Math.sqrt(2); // 1.414213562373095

Math.sqrt(1);  // 1
Math.sqrt(0);  // 0
Math.sqrt(-1); // NaN

Array.prototype.every()

배열안에 모든 요소가 주어진 판별 함수를 통과하는지 테스트

function isBelowThreshold(currentValue) {
  return currentValue < 40
}

var array1 = [1, 30, 39, 29, 10, 13]
var array2 = [1, 30, 52, 29, 10, 13]

console.log(array1.every(isBelowThreshold)) // true
console.log(array2.every(isBelowThreshold)) // false

'Study > Today I Learn' 카테고리의 다른 글

[TIL] 2019.04.08 - 다각형의 넓이 구하기  (0) 2019.04.09
[TIL] 2019.04.08 - debug  (0) 2019.04.08
[TIL] 2019.04.05  (0) 2019.04.05
[TIL] 2019.04.04 - Pull Request  (0) 2019.04.05
[TIL] 2019.04.04 - Git 기본  (0) 2019.04.04