[JavaScript] ๋ผ์ด๋ธ ์ฝ๋ฉํ ์คํธ ์์ ์ ๋ฆฌ
25+ JavaScript Coding Interview Questions (SOLVED with CODE) | FullStack.Cafe
25+ JavaScript Coding Interview Questions (SOLVED with CODE) | FullStack.Cafe
Having a JavaScript Coding Interview Session on this week? Fear not, we got your covered! Check that ultimate list of 25 advanced and tricky JavaScript Coding Interview Questions and Challenges to crack on your next senior web developer interview and got y
www.fullstack.cafe
[JavaScript] ๊ธฐ์ ๋ฉด์ ๋ผ์ด๋ธ ์ฝ๋ฉ ๋๋น I (velog.io)
[JavaScript] ๊ธฐ์ ๋ฉด์ ๋ผ์ด๋ธ ์ฝ๋ฉ ๋๋น I
1. concat ๋ฌธ์ ๋ต 2. anonymous function ๋ฌธ์ ๋ต 3. closure ๋ฌธ์ ๋ต 4. ๋น๋๊ธฐ 5. push, pop, min 6. array ์ค๋ณต ์์ ์ ๊ฑฐ 7. str ์ค๋ณต ์์ ์ ๊ฑฐ 8. ๋ฌธ์์ด ๋ค์ง๊ธฐ ๋ต 9. Mat
velog.io
๋ผ์ด๋ธ ์ฝ๋ฉํ ์คํธ๋ฅผ ์๋๊ณ ์ข์ ์์ ๋ค์ ๋ฐ๊ฒฌํด ํ๋ฒ ์ ๋ฆฌ๋ฅผ ํด๋ณด๋ ค๊ณ ํฉ๋๋ค.
๊ณผ์ฐ ์ด ๋ฌธ์ ๋ค์ด ๋์ฌ์ง๋ ๋ชจ๋ฅด๊ฒ ์ง๋ง.. ๋๋ฌด ๋๋ ค์์

1. Explain what a callback function is and provide a simple example
์ฝ๋ฐฑํจ์๋ ๋ค๋ฅธ ํจ์์ ์ ๋ฌ๋์ด ์ผ๋ถ ์์ ์ด ์๋ฃ๋ ํ ํธ์ถ๋๋ ํจ์์ด๋ค.
function modifyArray(arr, callback) {
//์ผ๋ถ ์์
arr.push(100);
//์ฝ๋ฐฑ ํจ์ ํธ์ถ
callback();
}
const arr = [1,2,3,4,5];
modifyArray(arr, function() {
//์ต๋ช
ํจ์๋ก ์ฝ๋ฐฑํจ์ ์ ๋ฌ
console.log('array has been modified', arr); //"array has been modified" [1,2,3,4,5,100]
});
2.Given a string, reverse each word in the sentence
[๋ฌธ์ ]
"Hello! Welcome to my Velog. Ask me anything.";
ํด๋น ๋ฌธ์์ด์ ๊ธ์๋ณ๋ก ๋ค์ง๊ธฐ, ๋จ์ด๋ณ๋ก ๋ค์ง๊ธฐ๋ฅผ ์ถ๋ ฅํ์ธ์.
[๋ต]
let str = "Hello! Welcome to my Velog. Ask me anything.";
function reverseBySeparator(str, separator) {
return str.split(separator).reverse().join(separator);
}
let reverseEntireSentence = reverseBySeparator(str, "");
let reverseEachWord = reverseBySeparator(str, " ");
console.log(reverseEntireSentence, reverseEachWord);
3. How to check if an object is an arrray of not? Provied some code.
์๋ฐ์คํฌ๋ฆฝํธ์์ ํจ์๋ฅผ ํธ์ถํ๋ ๋ฐฉ๋ฒ์๋,
1. ํจ์ ๋ค์ ()๋ฅผ ๋ถ์ฌ ํธ์ถํ๋ ๋ฐฉ๋ฒ
2. call(), apply()๋ฅผ ์ฌ์ฉํ์ฌ ํธ์ถํ๋ ๋ฐฉ๋ฒ์ด ์๋ค. => this๋ฅผ ๋ช ์์ ๋ฐ์ธ๋ฉ ํด์ฃผ๋ ๋ฉ์๋
this๋ ๊ธฐ๋ณธ์ ์ผ๋ก ์ ์ญ๊ฐ์ฒด์ธ window๋ฅผ ๊ฐ๋ฅดํค๊ฑฐ๋ ๋ฉ์๋๋ฅผ ํธ์ถํ ๊ฐ์ฒด๋ฅผ ๊ฐ๋ฅดํจ๋ค.
apply()์ call() ๋ฉ์๋๋ Function Object์ ๊ธฐ๋ณธ์ ์ผ๋ก ์ ์๋ ๋ฉ์๋๋ก, ์ฒซ๋ฒ์งธ ๋งค๊ฐ๋ณ์๋ฅผ this๋ก ๋ง๋ค์ด์ฃผ๋ ๊ธฐ๋ฅ์ ํ๋ค.
- Function.prototype.call()
func.call(thisArg[, arg1[, arg2[, ...]]])
- thisArg: func ํธ์ถ์ ์ ๊ณต๋๋ this์ ๊ฐ
- arg1, arg2, ...: func์ด ํธ์ถ๋์ด์ผ ํ๋ ์ธ์
์ฒซ๋ฒ์งธ ๋งค๊ฐ๋ณ์๋ this๋ฅผ ์ง์ ํ๋ ๊ฐ์ด ๋ค์ด๊ฐ์ผํ๊ณ , ๋๋ฒ์งธ ๋งค๊ฐ๋ณ์๋ ํธ์ถํ ํจ์์ ์ธ์๋ค์ด ๋ค์ด๊ฐ์ผํ๋ค.
let person1 = {
name: 'Jo'
};
let person2 = {
name: 'Kim',
study: function() {
console.log(this.name + '์ด/๊ฐ ๊ณต๋ถ๋ฅผ ํ๊ณ ์์ต๋๋ค.');
}
};
person2.study(); // Kim์ด/๊ฐ ๊ณต๋ถ๋ฅผ ํ๊ณ ์์ต๋๋ค.
// call()
person2.study.call(person1); // Jo์ด/๊ฐ ๊ณต๋ถ๋ฅผ ํ๊ณ ์์ต๋๋ค.
์์ ๊ฐ์ด ์๋๋ผ๋ฉด study()๋ฅผ ํธ์ถํ person2์ name์ธ Kim์ ๊ฐ๋ฆฌ์ผ์ผํ์ง๋ง
call() ๋ฉ์๋๋ฅผ ํตํด this๊ฐ person1์ ๊ฐ๋ฆฌํค๋ฉด์ this.name์ ๊ฐ์ด Jo๊ฐ ๋๋ค.
function HowisThis() {
console.log(this);
}
HowisThis(); //window
var obj = {
x: great,
};
HowisThis.call(obj); //{x:great} HowisThis.apply(obj)๋ก ์จ๋ ๋๊ฐ์ต๋๋ค.
ํจ์๋ฅผ ํธ์ถํ๋ฉด this๋ฅผ ์ฝ์์ ์ฐ๋๋ฐ, ํจ์์ ์ฃผ์ธ์ธ window ์ ์ญ๊ฐ์ฒด๊ฐ ์ญ์ ์ฐํ๋ค.
ํ์ง๋ง call() ๋ฉ์๋๋ฅผ ํตํด this๋ฅผ obj ๊ฐ์ฒด๋ก ๋ฐ์ธ๋ฉํด์ฃผ๋ฉด {x: great} ์ฆ, ๊ฐ์ฒด obj๊ฐ ์ฝ์์ ์ฐํ๋ค.
- Function.prototype.apply()
fun.apply(thisArg, [argsArray])
- thisArg: func ํธ์ถ์ ์ ๊ณต๋๋ this์ ๊ฐ
- argsArray: func์ด ํธ์ถ๋์ด์ผ ํ๋ ์ธ์๋ฅผ ์ง์ ํ๋ ์ ์ฌ ๋ฐฐ์ด ๊ฐ์ฒด
call() ๋ฉ์๋์ ๊ฐ์ด this๋ฅผ ์ง์ ํ๋ ๋ฉ์๋์ง๋ง, ์ฐจ์ด์ ์ ๋๋ฒ์งธ ๋งค๊ฐ๋ณ์๊ฐ ๋ฐฐ์ด ํํ๋ก ๋ค์ด๊ฐ์ผํ๋ค๋ ์ ์ด๋ค.
(๋ฐฐ์ด ๋๋ ์ ์ฌ๋ฐฐ์ด ๊ฐ์ฒด)
function add(c, d) {
return this.a + this.b + c + d;
}
var o = {a: 3, b: 3};
// ์ฒซ ๋ฒ์งธ ์ธ์๋ 'this'๋ก ์ฌ์ฉํ ๊ฐ์ฒด์ด๊ณ ,
// ์ด์ด์ง๋ ์ธ์๋ค์ ํจ์ ํธ์ถ์์ ์ธ์๋ก ์ ๋ฌ๋๋ค.
add.call(o, 6, 6); // 18
// ์ฒซ ๋ฒ์งธ ์ธ์๋ 'this'๋ก ์ฌ์ฉํ ๊ฐ์ฒด์ด๊ณ ,
// ๋ ๋ฒ์งธ ์ธ์๋ ํจ์ ํธ์ถ์์ ์ธ์๋ก ์ฌ์ฉ๋ ๋ฉค๋ฒ๋ค์ด ์์นํ ๋ฐฐ์ด์ด๋ค.
add.apply(o, [20, 10]); // 36
toString() method๋ ๊ฐ์ฒด๋ฅผ ๋ํ๋ด๋ ๋ฌธ์์ด์ ๋ฐํํ๋ค.
๊ทธ๋ฆฌ๊ณ ๊ธฐ๋ณธ์ ์ผ๋ก ๋ชจ๋ object์ ์์๋์ด ์๋ค.
method๊ฐ custom object์ ์ํด ์ฌ์ ์๋์ง ์์ผ๋ฉด,
toString();์ [object type]์ ๋ฐํํ๋ค. ์ฌ๊ธฐ์ type์ object type์ ์๋ฏธํ๋ค.
function person(name, age) {
this.name = name;
this.age = age;
}
const thePerson = new person('conut', 30);
console.log(thePerson);
console.log(thePerson.toString()); // return [object Object]
=> Object.prototype.toString์ ํตํด object type์ ๋ฐํํด์ array์ธ์ง ์๋์ง๋ฅผ ํ๋ณ ํ ์ ์๋ค.
const arr = [];
//call์ ์์ฐ๋ฉด ์ ์ญ๊ฐ์ฒด๋ฅผ ๊ฐ๋ฆฌ์ผ์ ์ฐ๋๊ฑด๊ฐ?
console.log(Object.prototype.toString(arr)); //"[object Object]"
console.log(Object.prototype.toString.call(arr)); //"[object Array]"
[๋ต]
const arr = [];
const number = 18;
//1.solution
function isArray(arr){
if(Object.prototype.toString.call(arr) === '[object Array]'){
console.log('yes');
}else{
console.log('no');
}
}
isArray(arr); //yes
isArray(18); //no
//2.solution
function isArray2(arr){
console.log(Array.isArray(arr));
}
isArray2(arr); //true
isArray2(number); //false
- Object.prototype.toString.call(array)
- Array.isArray(array)
๋๊ฐ์ง์ ๋ฐฉ๋ฒ์ด ์๋ค.
4. How to empty an array in JavaScript?
- solution 1
let arrayList = ['a','b','c','d','e','f'];
//solution 1
arrayList = [];
console.log(arrayList); // []
ํ์ง๋ง ํด๋น ๋ฐฉ๋ฒ์ ๋ค๋ฅธ ๊ณณ์ ์๋ ๋ฐฐ์ด์ ๋ํ ์ฐธ์กฐ๊ฐ ์๋ ๊ฒฝ์ฐ์๋ง ๊ถ์ฅ๋๋ค.
let arrayList = ['a','b','c','d','e','f'];
let anotherArrayList = arrayList;
arrayList = [];
console.log(anotherArrayList); //['a','b','c','d','e','f']
- solution 2
let arrayList = ['a','b','c','d','e','f'];
let anotherArrayList = arrayList;
arrayList.length = 0;
console.log(anotherArrayList); // []
์์ ๊ฐ์ด ์๋ ๋ฐฐ์ด์ ๊ธธ์ด๋ฅผ 0์ผ๋ก ์ค์ ์ฃผ๋ฉด ์๋ ๋ฐฐ์ด์ ๊ฐ๋ฆฌํค๋ ๋ชจ๋ ์ฐธ์กฐ ๋ณ์๋ฅผ ์ ๋ฐ์ดํธํ๋ค.
- solution 3
let arrayList = ['a','b','c','d','e','f'];
let anotherArrayList = arrayList;
arrayList.splice(0, arrayList.length); //์๋ณธ ๋ฐฐ์ด์ ์์
console.log(anotherArrayList, arrayList); //[],[]
ํด๋น ๋ฐฉ๋ฒ๋ ์๋ฒฝํ๊ฒ ์๋ํ๋ค. arrayList์ ์๋ณธ์ ์๋ผ์ ๋ค ๋ ๋ฆฌ๋ ๋ฐฉ๋ฒ์ธ ๊ฒ๊ฐ๋ค.
- solution 4
let arrayList = ['a','b','c','d','e','f'];
//arrayList๊ฐ ๋น ๋๊น์ง ๊ณ์ pop
while(arrayList.length){
arrayList.pop();
}
console.log(arrayList); // []
5. How would you check if a number is an integer?
์ด ๋ฐฉ๋ฒ์ ๊ฐ๋จํ๋ค. 1๋ก ๋๋ด์ ๋ ๋๋จธ์ง๊ฐ 0์ด๋ฉด ์ ์์ด๋ค.
function isInt(num){
console.log(num % 1 === 0 ? true : false);
}
isInt(1); //true
isInt(0.1); //false
6. Implement enqueue and dequeue using only two stacks
enqueue๋ push์ ๊ฐ์ด ๋ค๋ก ์ถ๊ฐํ๊ณ , dequeue๋ ๊ฐ์ฅ ์์ ์์๊ฐ ๋น ์ง๋ ๊ฒ์ด๋ค.
const inputStack = [];
const outputStack = [];
function enqueue(stackInput, item){
return stackInput.push(item);
}
function dequeue(stackInput, stackOutput){
//๊ธธ์ด๊ฐ 0๋ณด๋ค ์๊ฑฐ๋ ๊ฐ์ผ๋ฉด?
if(stackOutput.length <= 0){
while(stackInput.length > 0){
//inputStack์ ์๋ ๊ฑธ ๋นผ์ ๋ค stackOutput์ ๋ฃ์ด
//๊ทธ๋ผ ๊ฑฐ๊พธ๋ก ๋ค์ด๊ฐ๋ ๊ฑฐ๋ ๋ค๋ฅด์ง์์
let elementToOut = stackInput.pop();
stackOutput.push(elementToOut);
}
}
//0๋ณด๋ค ํฌ๋ฉด
//๊ทธ๋ผ ๊ฑฐ๊พธ๋ก ๋ค์ด๊ฐ์ผ๋๊น popํด์ฃผ๋ฉด ๊ฐ์ฅ ์์ ์์๋ฅผ ๋นผ์ค ์ ์๋๊ฑฐ์ง..
return stackOutput.pop();
}
7. Make this work
[๋ฌธ์ ]
duplicate([1, 2, 3, 4, 5]); // [1,2,3,4,5,1,2,3,4,5]
[๋ต]
const arr = [1,2,3,4,5];
function duplicate(arr){
//concat
console.log(arr.concat(arr));
//spread operator
console.log([...arr,...arr]);
}
duplicate(arr);
- concat
- ๊ธฐ์กด์ ๋ฐฐ์ด์ ๋ณ๊ฒฝํ์ง ์์ผ๋ฉด์ ๋ฐฐ์ด ๋์ ์์๋ฅผ ์ถ๊ฐํด ์ ๋ฐฐ์ด์ ๋ฐํํ๋ค.
const arr = [1,2,3,4,5];
const newArr = arr.concat(6);
console.log(arr, newArr); //[1,2,3,4,5] [1,2,3,4,5,6]
- spread operator(...)
- ๋ฐฐ์ด or ๋ฌธ์์ด or ๊ฐ์ฒด ์์ ํ๋๋ฅผ ํผ์ณ ๋ณต์ฌํด์ ๋ฐ๋ก ๋ถ๋ฌ์จ๋ค.
- ํฐ ๋ฐฐ์ด๋ก ์์ ํ ๋ ๋ฉ๋ชจ๋ฆฌ ๋ถ์กฑ์ผ๋ก ์ค๋ฅ๊ฐ ๋ฐ์ํ ์ ์๋ค.
8.Write a "mul" function which will properly when invoked as below syntax
[๋ฌธ์ ]
console.log(mul(2)(3)(4)); //24
console.log(mul(4)(3)(4)); //48
closure๋ฅผ ์ฌ์ฉํด์ ๊ตฌํ์ ํ ์ ์๋ค.
ํจ์ ๋ด์์ ํจ์๋ฅผ ๋ฐ๋ก ์คํ์ํค๋ ๋์ ๋ฐํ๊ฐ์ผ๋ก ํจ์๋ฅผ ๋ด๋ณด๋ธ๋ค.
[๋ต]
function mul(x){
return function(y){
return function(z){
return x * y * z;
}
}
}
9. Write a function that would allow you to do this?
[๋ฌธ์ ]
let addSix = createBase(6);
addSix(10); // 16
addSix(21); // 27
[๋ต]
ํด๋น ๋ฌธ์ ๋ก closure๋ฅผ ์ฌ์ฉํด์ ํจ์ ๋ฐ์ ์ ์ธ๋ ์๋ฅผ ๊ธฐ์ตํ๊ณ ๊ฐ์ ธ์จ๋ค.
function createBase(baseNumber){
return function(N){
return baseNumber + N;
}
}
10. FizzBuzz Challenge
100๊น์ง ๋ฐ๋ณต๋ฌธ์ ๋๋ฆฌ๋ฉด์ 3์ ๋ฐฐ์์ผ ๋๋ fizz๋ฅผ ์ถ๋ ฅํ๊ณ 5์ ๋ฐฐ์์ผ ๋๋ buzz๋ฅผ ์ถ๋ ฅํ๊ณ 3๊ณผ 5์ ๋ฐฐ์์ผ ๋๋ fizzbuzz๋ฅผ ์ถ๋ ฅํ๋ค.
[๋ต]
for(let i=1; i<=100; i++){
let f = i % 3 === 0;
let b = i % 5 === 0;
//3์ ๋ฐฐ์์ผ ๋ 5์ ๋ฐฐ์์ด๋ฉด fizzbuzz
//์๋๋ฉด ๊ทธ๋ฅ 3์ ๋ฐฐ์๋๊น fizz
//3์ ๋ฐฐ์์๋ ๋ 5์ ๋ฐฐ์์ด๋ฉด buzz
//๋๋ค ์๋๋ฉด ๊ทธ๋ฅ i
console.log(f ? (b ? "fizzbuz" : "fizz") : b ? "buzz" : i);
}
11. Given two strings, return true if they are anagrams of one another
์ ๋๊ทธ๋จ : ๊ฐ์ ๋จ์ด์ ๋ฌธ์๋ฅผ ์ฌ๋ฐฐ์ดํ์ฌ ๋ค๋ฅธ ๋ป์ ๊ฐ์ง๋ ๋ค๋ฅธ ๋จ์ด๋ก ๋ฐ๊พธ๋ ๊ฒ์ด๋ค.
๊ทธ๋์ ๋ ๋จ์ด๊ฐ ์๋ก์ ์ ๋๊ทธ๋จ์ด๋ฉด true๋ฅผ ๋ฆฌํดํด๋ผ.
[๋ต]
let firstWord = "Mary";
let secondWord = "Army";
function isAnagram(first, second){
let rowfirst = first.toLowerCase();
let rowsecond = second.toLowerCase();
rowfirst = [...rowfirst].sort().join('');
rowsecond = [...rowsecond].sort().join('');
console.log(rowfirst === rowsecond);
}
isAnagram(firstWord, secondWord);
12. How would you use a closure to create a private counter?
closer(ํจ์๋ฅผ return)๋ฅผ ์ด์ฉํด์ private ๋ณ์๋ฅผ ์ ๋ฐ์ดํธํด๋ผ.
์ธ๋ถํจ์๋ private ๋ณ์๋ฅผ ๊ฑด๋ค ์ ์๋ค.
function counter(){
let _counter = 0;
return {
add : function(increment) { _counter += increment },
retrieve : function() { console.log(`the counter is currently at ${_counter}`); }
}
}
let c = counter();
c.add(5);
c.add(9);
c.retrieve(); //"the counter is currently at 14"
13. Write a function that would allow you to do this
[๋ฌธ์ ]
multiply(5)(6); //30
[๋ต]
function multiply(a){
return function(b){
return a * b;
}
}
14. How does the this keyword work? Provide some code examples
function foo(){
console.log(this.bar);
}
var bar = 'global';
var obj1 = {
bar: 'obj1',
foo: foo
};
var obj2 = {
bar: 'obj2'
};
foo(); // ํจ์์ this๋ ์ ์ญ๊ฐ์ฒด๋ฅผ ๊ฐ๋ฅดํค๊ณ ๊ทธ๋ผ global
obj1.foo(); //obj1์ด ํธ์ถํ์ผ๋ obj1
foo.call(obj2); //obj2๋ก call() ๋ฉ์๋๋ฅผ ํตํด ๋ฐ์ธ๋ฉ ํด์คฌ์ผ๋ obj2
new foo(); //์์ฑ์ ํจ์๋ก ๋ง๋ ๊ฐ์ฒด๋ฅผ ๊ฐ๋ฅดํค๋๋ฐ ๊ฐ์ฒด ์์๋ bar๊ฐ ์์ผ๋ undefined?
15. How would you create a private variable in JavaScript?
[๋ฌธ์ ]
function func(){
let priv = "secret code";
}
console.log(priv) //throw Error
์์ ๊ฐ์ด let์ผ๋ก ๋ณ์๋ฅผ ์ ์ธํ๋ฉด let์ ๋ธ๋ก ์ค์ฝํ์ด๊ธฐ ๋๋ฌธ์ ๋ธ๋ก ๋ฐ์์๋ ์ ๊ทผํ ์๊ฐ ์๋ค.
[๋ต]
function func(){
let priv = "secret code";
return function(){
return priv;
}
}
let gerPriv = func();
console.log(gerPriv()); //secret code
16. When would you use the bind function?
let person1 = {
name: 'jo'
};
let person2 = {
name: 'kim',
study: function() {
console.log(this.name + '์ด/๊ฐ ๊ณต๋ถ๋ฅผ ํ๊ณ ์์ต๋๋ค.');
}
}
person2.study(); //kim์ด/๊ฐ ๊ณต๋ถ๋ฅผ ํ๊ณ ์์ต๋๋ค.
//๋ณ์๋ฅผ ํ ๋นํ์ฌ ํธ์ถํ๋ ํํ๋ก ์ฌ์ฉํด์ผํ๋ค.
let student = person2.study.bind(person1);
student(); //jo์ด/๊ฐ ๊ณต๋ถ๋ฅผ ํ๊ณ ์์ต๋๋ค.
17. Write a recursive function that performs a binary search
์ฌ๊ทํจ์๋ฅผ ํตํ ์ด์งํ์(์ ๋ ฌ๋ ๋ฆฌ์คํธ์์ ๊ฒ์ ๋ฒ์๋ฅผ ์ค์ฌ ๋๊ฐ๋ฉด์ ๊ฒ์ ๊ฐ์ ์ฐพ๋ ์๊ณ ๋ฆฌ์ฆ)
function recursiveBinarySearch(array, value, leftPosition, rightPosition){
//์ข
๋ฃ์กฐ๊ฑด(์ฐพ์ง ๋ชปํ์ ๋)
if(leftPosition > rightPosition) return -1;
let middlePivot = Math.floor((leftPosition + rightPosition) / 2);
if(array[middlePivot] === value){
return middlePivot;
}else if(array[middlePivot] > value){
//์ฐพ๋ value๊ฐ๋ณด๋ค ์ค์๊ฐ์ด ๋ ํฌ๋ฉด
return recursiveBinarySearch(array, value, leftPosition, middlePivot - 1);
}else{
//์ค์๊ฐ์ด ๋ ์์ผ๋ฉด
return recursiveBinarySearch(array, value, middlePivot + 1, rightPosition);
}
}
๋ฐ๋ณต๋ฌธ์ ์ด์ฉํ ๋ฐฉ๋ฒ๋ ์๋ค.
function binarySearch(array, value, leftPosition, rightPosition){
while(leftPosition <= rightPosition){
let mid = Math.floor((leftPosition + rightPosition)/2));
if(array[mid] === value){
return mid;
}else if(array[mid] > value){
rightPosition = mid - 1;
}else{
leftPosition = mid + 1;
}
}
//๋ชป์ฐพ์ผ๋ฉด
return -1;
}
18. closure
[๋ฌธ์ ]
sum(1)(2)(); //3
sum(1)(2)(3)(4); //10
[๋ต]
ํด๋ก์ ๋ ์ฌ๊ท๋ฅผ ์ด์ฉํด์ ํ์ดํ๋ฉด ๋๋ค.
function sum(a){
return function(b){
if(b){
return sum(a+b);
}else{
return a;
}
}
}
19. delay
[๋ฌธ์ ]
const delay = () => {}
const main = async() => {
console.log('1๋ฒ');
delay(4);
//4์ด ๋๋ ์ด ์ํจ ๋ค์์
console.log('2๋ฒ');
}
main();
[๋ต]
const delay = (n) => {
//resolve() ํจ์๋ฅผ ์คํํ๋ ๊ฒ ์์ฒด๋ง์ผ๋ก๋ Promise๋ fullfilled ์ํ๊ฐ ๋๋ค.
//๋น๋๊ธฐ ์ฒ๋ฆฌ๋ฅผ n์ด ์๋ค๊ฐ ์ฑ๊ณตํ๋๊ฑฐ๊ตฌ๋..
return new Promise((resolve) => setTimeout(resolve, n*1000));
}
const main = async() => {
console.log('1๋ฒ');
await delay(4);
//4์ด ๋๋ ์ด ์ํจ ๋ค์์
console.log('2๋ฒ');
}
main();
20. array ์ค๋ณต ์์ ์ ๊ฑฐ
[๋ต]
const exampleArray = [4, 2, 9, 2, 4, 6, 8, 9];
//indexOf๋ ๋งจ ์์ ์์์ ์ธ๋ฑ์ค๋ฅผ ๋ฆฌํดํ๊ธฐ ๋๋ฌธ์ ๋ค์ ์ค๋ณต ์์๋ฅผ ์ญ์ ํ ์ ์๋ค.
const uniqueArray = exampleArray.filter((v,i) => exampleArray.indexOf(v) === i);
console.log(uniqueArray);
const exampleArray = [4, 2, 9, 2, 4, 6, 8, 9];
return [...new Set(exampleArray)];
21. str ์ค๋ณต ์์ ์ ๊ฑฐ
const exampleStr = 'aabbbeedudaacca'
return [...new Set(exampleStr)].join('');
let exampleStr = 'aabbbeedudaacca';
exampleStr = [...exampleStr];
let uniquestr = exampleStr.filter((v,i) => exampleStr.indexOf(v) === i);
return uniquestr.join('');
22. Math.max
[๋ฌธ์ ]
์ซ์ํ array๋ฅผ ๋งค๊ฐ๋ณ์๋ก ๊ฐ๊ฒ ํ๊ณ , Math.max๋ฅผ ํ์ฉํด ์ต๋๊ฐ์ ์ถ๋ ฅํ์ธ์.
(๋จ, ์คํ๋ ๋ ์ฐ์ฐ์๋ฅผ ์ฐ์ง ์๊ณ )
๋ง์ฝ ์คํ๋ ๋ ์ฐ์ฐ์๋ฅผ ์ด๋ค๋ฉด
const exampleArray = [4, 2, 8, 1, 1, 3]
return Math.max(...exampleArray);
ํ์ง๋ง ์ฐ์ง ์๋๋ค๋ฉด,
apply() ๋ฉ์๋๋ฅผ ํตํด ๋ฐฐ์ดํํ๋ก ์ธ์๋ฅผ ๋๊ฒจ์ฃผ๊ฑฐ๋..(๊ทผ๋ฐ ์ด๊ฑด ๋๋ฌด ํฐ ๋ฐฐ์ด์ด๋ฉด ๋ฉ๋ชจ๋ฆฌ ์ด๊ณผ)
์๋๋ฉด reduce๋ฅผ ํตํด ๊ตฌํด์ค๋ค.
const exampleArray = [4, 2, 8, 1, 1, 3]
return Math.max.apply(null, exampleArray);
const exampleArray = [4, 2, 8, 1, 1, 3]
//๊ณ์ ํฐ ๊ฐ์ ๋์
ํ๋ฉด์ max๋ฅผ ์ฐพ์๋ธ๋ค.
return exampleArray.reduce((a,b) => Math.max(a,b), -Infinity);