Skip to content

Instantly share code, notes, and snippets.

@Beomi
Created July 10, 2017 13:22
Show Gist options
  • Save Beomi/059d7f8d929bd141a8ea36754f868f31 to your computer and use it in GitHub Desktop.
Save Beomi/059d7f8d929bd141a8ea36754f868f31 to your computer and use it in GitHub Desktop.
우아한 테크캠프 2주 1일차 과제: `map`/`filter`구현하기 | 어쩌다 JS는 JS가 되었나 | `!!`은 무엇인가
// map function
const mapFunction = (array, func) => {
const new_array = []
for (i of array) {
new_array.push(
func(i)
)
}
return new_array
}
// map test function
const mapTestFunction = (i) => {
return i ** 2
}
// map test code
const arr1 = [2,3,4,5,6]
const map_test_result = mapFunction(arr1, mapTestFunction)
console.log(map_test_result)
const real_map_result = arr1.map(mapTestFunction)
console.log(real_map_result)
// filter function
const filterFunction = (array, func) => {
const new_array = []
for (i of array) {
if (!!func(i)) {
new_array.push(i)
}
}
return new_array
}
// filter test function
const filterTestFunction = (v) => {
if (v < 5) {
return true
} else {
return false
}
}
// filter test code
const arr2 = ["", 0, "true", "jk", undefined, null, false];
const filter_test_result = filterFunction(arr2, filterTestFunction)
console.log(filter_test_result)
const real_filter_result = arr2.filter(filterTestFunction)
console.log(real_filter_result)

!!은 무엇인가?


JS에서의 !

JavaScript에서 !not의 의미이고, Bool 연산시 true -> false, false -> true로 변환해준다.

그리고 특정한 값 혹은 객체는 false와 동등하게 취급된다. 즉, if구문의 조건의 결과로 나올 경우 '', null, undefined, []등은 false로 취급되는 것이다.

하지만 null등이 false와 같지는 않다.

경우에 따라 이러한 값을 명시적으로 truefalse등으로 비교를 해주는 작업이 필요한데, 이 때 사용할 수 있는 것이 !!이다.

!을 사용할 경우 JS에서는 값 혹은 객체를 Bool 타입으로 형변환을 진행한다. 따라서 이떄의 결과값은 true혹은 false가 되는데, 이를 다시 돌려주기 위해 !!으로 !을 두번 붙여주는 것이다.

JavaScript는 왜 'Java'Script인가?


JS는 Mocha에서 시작했지만 이후 넷스케이프의 LiveScript가 됨.

이후 넷스케이프와 SUN이 'java'에 대해 라이센스 계약을 맺고 JS가 됨.

컴파일 언어인 JAVA를 보완하는 스크립트형 언어로 만들자는 의도가 있다.

@Beomi
Copy link
Author

Beomi commented Jul 10, 2017

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment