from. programmers.com algorithm

sort

๋‚˜๋ˆ„์–ด ๋–จ์–ด์ง€๋Š” ์ˆซ์ž ๋ฐฐ์—ด

๋ฌธ์ œ ์„ค๋ช…

array์˜ ๊ฐ element ์ค‘ divisor๋กœ ๋‚˜๋ˆ„์–ด ๋–จ์–ด์ง€๋Š” ๊ฐ’์„ ์˜ค๋ฆ„์ฐจ์ˆœ์œผ๋กœ ์ •๋ ฌํ•œ ๋ฐฐ์—ด์„ ๋ฐ˜ํ™˜ํ•˜๋Š” ํ•จ์ˆ˜, solution์„ ์ž‘์„ฑํ•ด์ฃผ์„ธ์š”.
divisor๋กœ ๋‚˜๋ˆ„์–ด ๋–จ์–ด์ง€๋Š” element๊ฐ€ ํ•˜๋‚˜๋„ ์—†๋‹ค๋ฉด ๋ฐฐ์—ด์— -1์„ ๋‹ด์•„ ๋ฐ˜ํ™˜ํ•˜์„ธ์š”.

์ œํ•œ์‚ฌํ•ญ

  • arr์€ ์ž์—ฐ์ˆ˜๋ฅผ ๋‹ด์€ ๋ฐฐ์—ด์ž…๋‹ˆ๋‹ค.
  • ์ •์ˆ˜ i, j์— ๋Œ€ํ•ด i ≠ j ์ด๋ฉด arr[i] ≠ arr[j] ์ž…๋‹ˆ๋‹ค.
  • divisor๋Š” ์ž์—ฐ์ˆ˜์ž…๋‹ˆ๋‹ค.
  • array๋Š” ๊ธธ์ด 1 ์ด์ƒ์ธ ๋ฐฐ์—ด์ž…๋‹ˆ๋‹ค.

์ž…์ถœ๋ ฅ ์˜ˆarrdivisorreturn[5, 9, 7, 10]5[5, 10][2, 36, 1, 3]1[1, 2, 3, 36][3,2,6]10[-1]์ž…์ถœ๋ ฅ ์˜ˆ ์„ค๋ช…

์ž…์ถœ๋ ฅ ์˜ˆ#1
arr์˜ ์›์†Œ ์ค‘ 5๋กœ ๋‚˜๋ˆ„์–ด ๋–จ์–ด์ง€๋Š” ์›์†Œ๋Š” 5์™€ 10์ž…๋‹ˆ๋‹ค. ๋”ฐ๋ผ์„œ [5, 10]์„ ๋ฆฌํ„ดํ•ฉ๋‹ˆ๋‹ค.

์ž…์ถœ๋ ฅ ์˜ˆ#2
arr์˜ ๋ชจ๋“  ์›์†Œ๋Š” 1์œผ๋กœ ๋‚˜๋ˆ„์–ด ๋–จ์–ด์ง‘๋‹ˆ๋‹ค. ์›์†Œ๋ฅผ ์˜ค๋ฆ„์ฐจ์ˆœ์œผ๋กœ ์ •๋ ฌํ•ด [1, 2, 3, 36]์„ ๋ฆฌํ„ดํ•ฉ๋‹ˆ๋‹ค.

์ž…์ถœ๋ ฅ ์˜ˆ#3
3, 2, 6์€ 10์œผ๋กœ ๋‚˜๋ˆ„์–ด ๋–จ์–ด์ง€์ง€ ์•Š์Šต๋‹ˆ๋‹ค. ๋‚˜๋ˆ„์–ด ๋–จ์–ด์ง€๋Š” ์›์†Œ๊ฐ€ ์—†์œผ๋ฏ€๋กœ [-1]์„ ๋ฆฌํ„ดํ•ฉ๋‹ˆ๋‹ค.

1
2
3
4
5
6
7
8
9
10
11
12
function solution(arr, divisor) {
   let result =[]
   for(let i =0; i<arr.length; i++){
       if(arr[i] % divisor === 0){
            result.push(arr[i])
        }
   }
    if(result.length ===0){
        return result = [-1]
    }
    return result.sort((a,b)=>a-b);
}
cs

๋ฌธ์ž์—ด ๋‚ด๋ฆผ์ฐจ์ˆœ์œผ๋กœ ๋ฐฐ์น˜ํ•˜๊ธฐ

๋ฌธ์ œ ์„ค๋ช…

๋ฌธ์ž์—ด s์— ๋‚˜ํƒ€๋‚˜๋Š” ๋ฌธ์ž๋ฅผ ํฐ๊ฒƒ๋ถ€ํ„ฐ ์ž‘์€ ์ˆœ์œผ๋กœ ์ •๋ ฌํ•ด ์ƒˆ๋กœ์šด ๋ฌธ์ž์—ด์„ ๋ฆฌํ„ดํ•˜๋Š” ํ•จ์ˆ˜, solution์„ ์™„์„ฑํ•ด์ฃผ์„ธ์š”.
s๋Š” ์˜๋ฌธ ๋Œ€์†Œ๋ฌธ์ž๋กœ๋งŒ ๊ตฌ์„ฑ๋˜์–ด ์žˆ์œผ๋ฉฐ, ๋Œ€๋ฌธ์ž๋Š” ์†Œ๋ฌธ์ž๋ณด๋‹ค ์ž‘์€ ๊ฒƒ์œผ๋กœ ๊ฐ„์ฃผํ•ฉ๋‹ˆ๋‹ค.

์ œํ•œ ์‚ฌํ•ญ

  • str์€ ๊ธธ์ด 1 ์ด์ƒ์ธ ๋ฌธ์ž์—ด์ž…๋‹ˆ๋‹ค.

์ž…์ถœ๋ ฅ ์˜ˆsreturn

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
function solution(s) {
    let result =[];
    for(let i=0; i< s.length; i++){
        if(s[i] < s[i+1]){
           result.push(s[i])
    }
    
}
    return result.reverse().join('')
}
 
 
๋‚ด๋ฆผ์ฐจ์ˆœ
๋ฌธ์ž์—ด์ผ ๋•Œ,
function solution(s) {
    // 1. return s.split('').sort((a,b)=> b > a ? 1 : b < a ? -1 : 0).join('')
    // 2. return s.split('').sort().reverse().join('')
}
์ˆซ์ž์ผ ๋•Œ,
.sort((a,b) => a-b)
 
์˜ฌ๋ฆผ์ฐจ์ˆœ
๋ฌธ์ž์—ด์ผ ๋•Œ,
function solution(s) {
    // 1. return s.split('').sort((a,b)=> b < a ? 1 : b > a ? -1 : 0).join('')
    // 2. return s.split('').sort().reverse().join('')
}
์ˆซ์ž์ผ ๋•Œ,
.sort((a,b)=>b-a)
cs

 


๋ฌธ์ž์—ด ๋‹ค๋ฃจ๊ธฐ ๊ธฐ๋ณธ

๋ฌธ์ œ ์„ค๋ช…

๋ฌธ์ž์—ด s์˜ ๊ธธ์ด๊ฐ€ 4 ํ˜น์€ 6์ด๊ณ , ์ˆซ์ž๋กœ๋งŒ ๊ตฌ์„ฑ๋ผ์žˆ๋Š”์ง€ ํ™•์ธํ•ด์ฃผ๋Š” ํ•จ์ˆ˜, solution์„ ์™„์„ฑํ•˜์„ธ์š”. ์˜ˆ๋ฅผ ๋“ค์–ด s๊ฐ€ a234์ด๋ฉด False๋ฅผ ๋ฆฌํ„ดํ•˜๊ณ  1234๋ผ๋ฉด True๋ฅผ ๋ฆฌํ„ดํ•˜๋ฉด ๋ฉ๋‹ˆ๋‹ค.

์ œํ•œ ์‚ฌํ•ญ

  • s๋Š” ๊ธธ์ด 1 ์ด์ƒ, ๊ธธ์ด 8 ์ดํ•˜์ธ ๋ฌธ์ž์—ด์ž…๋‹ˆ๋‹ค.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
function solution(s) {
     if(s.length !== 4 && s.length !==6){
         return false;
     }
     return [...s].filter(function(el){
         return isNaN(el)
     }).length === 0 
 }
 
 
function solution(s) {
     if(s.length !== 4 && s.length !==6){
         return false;
     }
    for(let i = 0; i < s.length; i++){
             if(isNaN(s[i])) {
        return false ////๋ฐฐ์—ด์— ์•ŒํŒŒ๋ฒณ์ด ์žˆ๋Š” ๊ฒฝ์šฐ ๋ฐ˜ํ™˜ํ•˜๊ณ 
         }
         return true//์–ด๋–ค s[i]๋„ ์•ˆ๋‚˜์˜จ๋‹ค๋ฉด return true
//else๋กœ ์“ธ ์ˆ˜ ์—†๋Š” ์ด์œ ๋Š” ๋ฌธ์ž์—ด์„ ๋‹ค ์ˆœํ™˜ํ•ด์•ผ๋˜๊ธฐ ๋–„๋ฌธ
 }
cs

์ง€์ˆ˜ํ˜•์‹ = ‘1e22’๋ฅผ ์ „์ฒด๋กœ ์ˆซ์ž๋กœ ๋ณ€ํ™˜ํ•˜๋ฉด ์ˆซ์ž๊ฐ€ ๋จ

๊ทธ๋ ‡๊ธฐ์— isNaN์„ ์‚ฌ์šฉํ•ด์•ผ ๋จ!!!!!!!!!

isNaN ()===Number.isNaN()

isNaN(NaN์ด true์ผ ๋•Œ) => true

isNaN(NaN์ด false์ผ ๋•Œ) => false

1
2
3
4
5
let a='as11'
[...a]; //['a', 's', '1', '1']
 
isNaN('a'); // true
isNaN(Number('a'));// true
cs

isNaN์€ ์ž๋™์ ์œผ๋กœ ๋„˜๋ฒ„ ํƒ€์ž…์œผ๋กœ ๋ณ€ํ™˜ํ•ด์„œ ์‹คํ–‰ ๋จ!!


๊ฐ™์€ ์ˆซ์ž๋Š” ์‹ซ์–ด!!

๋ฌธ์ œ ์„ค๋ช…

๋ฐฐ์—ด arr๊ฐ€ ์ฃผ์–ด์ง‘๋‹ˆ๋‹ค. ๋ฐฐ์—ด arr์˜ ๊ฐ ์›์†Œ๋Š” ์ˆซ์ž 0๋ถ€ํ„ฐ 9๊นŒ์ง€๋กœ ์ด๋ฃจ์–ด์ ธ ์žˆ์Šต๋‹ˆ๋‹ค. ์ด๋•Œ, ๋ฐฐ์—ด arr์—์„œ ์—ฐ์†์ ์œผ๋กœ ๋‚˜ํƒ€๋‚˜๋Š” ์ˆซ์ž๋Š” ํ•˜๋‚˜๋งŒ ๋‚จ๊ธฐ๊ณ  ์ „๋ถ€ ์ œ๊ฑฐํ•˜๋ ค๊ณ  ํ•ฉ๋‹ˆ๋‹ค. ๋‹จ, ์ œ๊ฑฐ๋œ ํ›„ ๋‚จ์€ ์ˆ˜๋“ค์„ ๋ฐ˜ํ™˜ํ•  ๋•Œ๋Š” ๋ฐฐ์—ด arr์˜ ์›์†Œ๋“ค์˜ ์ˆœ์„œ๋ฅผ ์œ ์ง€ํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. ์˜ˆ๋ฅผ ๋“ค๋ฉด,

  • arr = [1, 1, 3, 3, 0, 1, 1] ์ด๋ฉด [1, 3, 0, 1] ์„ return ํ•ฉ๋‹ˆ๋‹ค.
  • arr = [4, 4, 4, 3, 3] ์ด๋ฉด [4, 3] ์„ return ํ•ฉ๋‹ˆ๋‹ค.

๋ฐฐ์—ด arr์—์„œ ์—ฐ์†์ ์œผ๋กœ ๋‚˜ํƒ€๋‚˜๋Š” ์ˆซ์ž๋Š” ์ œ๊ฑฐํ•˜๊ณ  ๋‚จ์€ ์ˆ˜๋“ค์„ return ํ•˜๋Š” solution ํ•จ์ˆ˜๋ฅผ ์™„์„ฑํ•ด ์ฃผ์„ธ์š”.

์ œํ•œ์‚ฌํ•ญ

  • ๋ฐฐ์—ด arr์˜ ํฌ๊ธฐ : 1,000,000 ์ดํ•˜์˜ ์ž์—ฐ์ˆ˜
  • ๋ฐฐ์—ด arr์˜ ์›์†Œ์˜ ํฌ๊ธฐ : 0๋ณด๋‹ค ํฌ๊ฑฐ๋‚˜ ๊ฐ™๊ณ  9๋ณด๋‹ค ์ž‘๊ฑฐ๋‚˜ ๊ฐ™์€ ์ •์ˆ˜
  •  

์ž…์ถœ๋ ฅ ์˜ˆarranswer[1,1,3,3,0,1,1][1,3,0,1][4,4,4,3,3][4,3]

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
function solution(arr){
     var answer = [];
     for(let i =0; i<arr.length; i++){
         if(arr[i] !== arr[i+1]){
             answer.push(arr[i])
         }
     }
     return answer;
}
 
function solution(arr){
    return arr.filter(function(el, i){
        return arr[i] !== arr[i+1]
    })
}
 
function solution(arr){
    return arr.filter(function(el, i){
        return el !==arr[i+1]
    })
}
cs

map, forEach, filter๋Š” (value, index, array) 3๊ฐœ์˜ ์ธ์ˆ˜๋ฅผ ๋ฐ›์•„์˜ด.

value๋Š” array์˜ ์š”์†Œ์ด๋ฉฐ, index๋Š” array์˜ index์ด๊ณ , array๋Š” ๋ฐฐ์—ด ๊ทธ ์ž์ฒด์ด๋ฆ„์ด๋‹ค.

๋งŒ์•ฝ์— ๋ณ€์ˆ˜๋ช…์ด ๊ธธ์–ด์ง€๊ณ  ํ•จ์ˆ˜ ์•ˆ์— ์‚ฌ์šฉํ•˜๊ณ  ์‹ถ์œผ๋ฉด, ๊ฐ„๊ฒฐํ•˜๊ฒŒ ๋‹ค๋ฅธ ์ด๋ฆ„์œผ๋กœ ์ ๊ธฐ์œ„ํ•ด, ๋งค๊ฐœ๋ณ€์ˆ˜์— ์ด๋ฆ„์„ ์„ค์ •ํ•  ์ˆ˜ ์žˆ๋‹ค.

ex) const fjaiepfjwpifjwaefpwifj = [];

 


๋งค๊ฐœ๋ณ€์ˆ˜=ํ•จ์ˆ˜ ์•ˆ์— ๋“ค์–ด์™€์„œ ์ธ์ˆ˜๊ฐ€ ๋ถˆ๋ฆด ์ด๋ฆ„

์ธ์ˆ˜= ํ•จ์ˆ˜ ํ˜ธ์ถœํ•  ๋•Œ ๋„ฃ๋Š” ๊ฐ’


 

ํ•˜์ƒค๋“œ ์ˆ˜

๋ฌธ์ œ ์„ค๋ช…

์–‘์˜ ์ •์ˆ˜ x๊ฐ€ ํ•˜์ƒค๋“œ ์ˆ˜์ด๋ ค๋ฉด x์˜ ์ž๋ฆฟ์ˆ˜์˜ ํ•ฉ์œผ๋กœ x๊ฐ€ ๋‚˜๋ˆ„์–ด์ ธ์•ผ ํ•ฉ๋‹ˆ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด 18์˜ ์ž๋ฆฟ์ˆ˜ ํ•ฉ์€ 1+8=9์ด๊ณ , 18์€ 9๋กœ ๋‚˜๋ˆ„์–ด ๋–จ์–ด์ง€๋ฏ€๋กœ 18์€ ํ•˜์ƒค๋“œ ์ˆ˜์ž…๋‹ˆ๋‹ค. ์ž์—ฐ์ˆ˜ x๋ฅผ ์ž…๋ ฅ๋ฐ›์•„ x๊ฐ€ ํ•˜์ƒค๋“œ ์ˆ˜์ธ์ง€ ์•„๋‹Œ์ง€ ๊ฒ€์‚ฌํ•˜๋Š” ํ•จ์ˆ˜, solution์„ ์™„์„ฑํ•ด์ฃผ์„ธ์š”.

์ œํ•œ ์กฐ๊ฑด

  • x๋Š” 1 ์ด์ƒ, 10000 ์ดํ•˜์ธ ์ •์ˆ˜์ž…๋‹ˆ๋‹ค.

์ž…์ถœ๋ ฅ ์˜ˆarrreturn10true12true11false13false์ž…์ถœ๋ ฅ ์˜ˆ ์„ค๋ช…

์ž…์ถœ๋ ฅ ์˜ˆ #1
10์˜ ๋ชจ๋“  ์ž๋ฆฟ์ˆ˜์˜ ํ•ฉ์€ 1์ž…๋‹ˆ๋‹ค. 10์€ 1๋กœ ๋‚˜๋ˆ„์–ด ๋–จ์–ด์ง€๋ฏ€๋กœ 10์€ ํ•˜์ƒค๋“œ ์ˆ˜์ž…๋‹ˆ๋‹ค.

์ž…์ถœ๋ ฅ ์˜ˆ #2
12์˜ ๋ชจ๋“  ์ž๋ฆฟ์ˆ˜์˜ ํ•ฉ์€ 3์ž…๋‹ˆ๋‹ค. 12๋Š” 3์œผ๋กœ ๋‚˜๋ˆ„์–ด ๋–จ์–ด์ง€๋ฏ€๋กœ 12๋Š” ํ•˜์ƒค๋“œ ์ˆ˜์ž…๋‹ˆ๋‹ค.

์ž…์ถœ๋ ฅ ์˜ˆ #3
11์˜ ๋ชจ๋“  ์ž๋ฆฟ์ˆ˜์˜ ํ•ฉ์€ 2์ž…๋‹ˆ๋‹ค. 11์€ 2๋กœ ๋‚˜๋ˆ„์–ด ๋–จ์–ด์ง€์ง€ ์•Š์œผ๋ฏ€๋กœ 11๋Š” ํ•˜์ƒค๋“œ ์ˆ˜๊ฐ€ ์•„๋‹™๋‹ˆ๋‹ค.

์ž…์ถœ๋ ฅ ์˜ˆ #4
13์˜ ๋ชจ๋“  ์ž๋ฆฟ์ˆ˜์˜ ํ•ฉ์€ 4์ž…๋‹ˆ๋‹ค. 13์€ 4๋กœ ๋‚˜๋ˆ„์–ด ๋–จ์–ด์ง€์ง€ ์•Š์œผ๋ฏ€๋กœ 13์€ ํ•˜์ƒค๋“œ ์ˆ˜๊ฐ€ ์•„๋‹™๋‹ˆ๋‹ค.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
function solution(x) {
    x= String(x)
    let result =0;
    for(let i=0; i<x.length; i++){
       result += Number(x[i]);
   }
    return x % result === 0
    // if(x % result === 0){
    //        return true;
    // }
    // else{
    //     return false;
    // }
}
cs

Please write code simple!!!!!!!!!!!!


์ฒด์œก๋ณต

๋ฌธ์ œ ์„ค๋ช…

์ ์‹ฌ์‹œ๊ฐ„์— ๋„๋‘‘์ด ๋“ค์–ด, ์ผ๋ถ€ ํ•™์ƒ์ด ์ฒด์œก๋ณต์„ ๋„๋‚œ๋‹นํ–ˆ์Šต๋‹ˆ๋‹ค. ๋‹คํ–‰ํžˆ ์—ฌ๋ฒŒ ์ฒด์œก๋ณต์ด ์žˆ๋Š” ํ•™์ƒ์ด ์ด๋“ค์—๊ฒŒ ์ฒด์œก๋ณต์„ ๋นŒ๋ ค์ฃผ๋ ค ํ•ฉ๋‹ˆ๋‹ค. ํ•™์ƒ๋“ค์˜ ๋ฒˆํ˜ธ๋Š” ์ฒด๊ฒฉ ์ˆœ์œผ๋กœ ๋งค๊ฒจ์ ธ ์žˆ์–ด, ๋ฐ”๋กœ ์•ž๋ฒˆํ˜ธ์˜ ํ•™์ƒ์ด๋‚˜ ๋ฐ”๋กœ ๋’ท๋ฒˆํ˜ธ์˜ ํ•™์ƒ์—๊ฒŒ๋งŒ ์ฒด์œก๋ณต์„ ๋นŒ๋ ค์ค„ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด, 4๋ฒˆ ํ•™์ƒ์€ 3๋ฒˆ ํ•™์ƒ์ด๋‚˜ 5๋ฒˆ ํ•™์ƒ์—๊ฒŒ๋งŒ ์ฒด์œก๋ณต์„ ๋นŒ๋ ค์ค„ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์ฒด์œก๋ณต์ด ์—†์œผ๋ฉด ์ˆ˜์—…์„ ๋“ค์„ ์ˆ˜ ์—†๊ธฐ ๋•Œ๋ฌธ์— ์ฒด์œก๋ณต์„ ์ ์ ˆํžˆ ๋นŒ๋ ค ์ตœ๋Œ€ํ•œ ๋งŽ์€ ํ•™์ƒ์ด ์ฒด์œก์ˆ˜์—…์„ ๋“ค์–ด์•ผ ํ•ฉ๋‹ˆ๋‹ค.

์ „์ฒด ํ•™์ƒ์˜ ์ˆ˜ n, ์ฒด์œก๋ณต์„ ๋„๋‚œ๋‹นํ•œ ํ•™์ƒ๋“ค์˜ ๋ฒˆํ˜ธ๊ฐ€ ๋‹ด๊ธด ๋ฐฐ์—ด lost, ์—ฌ๋ฒŒ์˜ ์ฒด์œก๋ณต์„ ๊ฐ€์ ธ์˜จ ํ•™์ƒ๋“ค์˜ ๋ฒˆํ˜ธ๊ฐ€ ๋‹ด๊ธด ๋ฐฐ์—ด reserve๊ฐ€ ๋งค๊ฐœ๋ณ€์ˆ˜๋กœ ์ฃผ์–ด์งˆ ๋•Œ, ์ฒด์œก์ˆ˜์—…์„ ๋“ค์„ ์ˆ˜ ์žˆ๋Š” ํ•™์ƒ์˜ ์ตœ๋Œ“๊ฐ’์„ return ํ•˜๋„๋ก solution ํ•จ์ˆ˜๋ฅผ ์ž‘์„ฑํ•ด์ฃผ์„ธ์š”.

์ œํ•œ์‚ฌํ•ญ

  • ์ „์ฒด ํ•™์ƒ์˜ ์ˆ˜๋Š” 2๋ช… ์ด์ƒ 30๋ช… ์ดํ•˜์ž…๋‹ˆ๋‹ค.
  • ์ฒด์œก๋ณต์„ ๋„๋‚œ๋‹นํ•œ ํ•™์ƒ์˜ ์ˆ˜๋Š” 1๋ช… ์ด์ƒ n๋ช… ์ดํ•˜์ด๊ณ  ์ค‘๋ณต๋˜๋Š” ๋ฒˆํ˜ธ๋Š” ์—†์Šต๋‹ˆ๋‹ค.
  • ์—ฌ๋ฒŒ์˜ ์ฒด์œก๋ณต์„ ๊ฐ€์ ธ์˜จ ํ•™์ƒ์˜ ์ˆ˜๋Š” 1๋ช… ์ด์ƒ n๋ช… ์ดํ•˜์ด๊ณ  ์ค‘๋ณต๋˜๋Š” ๋ฒˆํ˜ธ๋Š” ์—†์Šต๋‹ˆ๋‹ค.
  • ์—ฌ๋ฒŒ ์ฒด์œก๋ณต์ด ์žˆ๋Š” ํ•™์ƒ๋งŒ ๋‹ค๋ฅธ ํ•™์ƒ์—๊ฒŒ ์ฒด์œก๋ณต์„ ๋นŒ๋ ค์ค„ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.
  • ์—ฌ๋ฒŒ ์ฒด์œก๋ณต์„ ๊ฐ€์ ธ์˜จ ํ•™์ƒ์ด ์ฒด์œก๋ณต์„ ๋„๋‚œ๋‹นํ–ˆ์„ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์ด๋•Œ ์ด ํ•™์ƒ์€ ์ฒด์œก๋ณต์„ ํ•˜๋‚˜๋งŒ ๋„๋‚œ๋‹นํ–ˆ๋‹ค๊ณ  ๊ฐ€์ •ํ•˜๋ฉฐ, ๋‚จ์€ ์ฒด์œก๋ณต์ด ํ•˜๋‚˜์ด๊ธฐ์— ๋‹ค๋ฅธ ํ•™์ƒ์—๊ฒŒ๋Š” ์ฒด์œก๋ณต์„ ๋นŒ๋ ค์ค„ ์ˆ˜ ์—†์Šต๋‹ˆ๋‹ค.

์ž…์ถœ๋ ฅ ์˜ˆnlostreservereturn5[2, 4][1, 3, 5]55[2, 4][3]43[3][1]2์ž…์ถœ๋ ฅ ์˜ˆ ์„ค๋ช…

์˜ˆ์ œ #1
1๋ฒˆ ํ•™์ƒ์ด 2๋ฒˆ ํ•™์ƒ์—๊ฒŒ ์ฒด์œก๋ณต์„ ๋นŒ๋ ค์ฃผ๊ณ , 3๋ฒˆ ํ•™์ƒ์ด๋‚˜ 5๋ฒˆ ํ•™์ƒ์ด 4๋ฒˆ ํ•™์ƒ์—๊ฒŒ ์ฒด์œก๋ณต์„ ๋นŒ๋ ค์ฃผ๋ฉด ํ•™์ƒ 5๋ช…์ด ์ฒด์œก์ˆ˜์—…์„ ๋“ค์„ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

์˜ˆ์ œ #2
3๋ฒˆ ํ•™์ƒ์ด 2๋ฒˆ ํ•™์ƒ์ด๋‚˜ 4๋ฒˆ ํ•™์ƒ์—๊ฒŒ ์ฒด์œก๋ณต์„ ๋นŒ๋ ค์ฃผ๋ฉด ํ•™์ƒ 4๋ช…์ด ์ฒด์œก์ˆ˜์—…์„ ๋“ค์„ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
function solution(n, lost, reserve) {
  const obj = {};
 
  for (let i = 1; i <= n; i++) {
    obj[i] = 1//๋ชจ๋“  ์• ๋“คํ•œํ…Œ ํ•œ ๋ฒŒ์”ฉ ์ฃผ๊ธฐ
  }
 
  for (let i = 0; i < reserve.length; i++) {
    obj[reserve[i]] += 1// ์—ฌ๋ถ„ ์žˆ๋Š” ์• ๋“คํ•œํ…Œ๋Š” ํ•œ ๋ฒŒ ๋” ์ถ”๊ฐ€
  }
 
  for (let i = 0; i < lost.length; i++) {
    obj[lost[i]] -= 1;// ์•ˆ๊ฐ€์ ธ์˜จ ์• ๋“ค์€ ํ•œ ๋ฒŒ๋นผ๊ธฐ
  }
 
  const arr = Object.values(obj); //value๋กœ๋งŒ ๋ฐฐ์—ด ๋งŒ๋“ค๊ธฐ
 
  for (let i = 0; i < arr.length; i++) {
    if (arr[i] === 2) { //์—ฌ๋ถ„์ด ์žˆ๋‹ค๋ฉด
      if (arr[i - 1=== 0) {// ์•ž์— ์• ๊ฐ€ ์•ˆ๊ฐ€์ ธ์˜จ ์• ๋ผ๋ฉด
        arr[i] -= 1//๋‚ด๊ฐ€ ์—ฌ๋ถ„์„ ์ค˜์•ผํ•˜๋‹ˆ ํ•œ ๋ฒŒ ๋นผ๊ณ 
        arr[i - 1+= 1// ์•ˆ๊ฐ€์ ธ์™”๋Š”๋ฐ ์—ฌ๋ฒŒ์„ ๋ฐ›์•˜์œผ๋‹ˆ ์ถ”๊ฐ€
      }
      else if (arr[i + 1=== 0) { // ๋‚ด ๋’ค์— ์• ๊ฐ€ ์—†๋‹ค๋ฉด
        arr[i] -= 1//๋‚ด๊ฐ€ ์—ฌ๋ถ„์„ ์ค˜์•ผํ•˜๋‹ˆ ํ•œ ๋ฒŒ ๋นผ๊ณ 
        arr[i + 1+= 1// ์•ˆ๊ฐ€์ ธ์™”๋Š”๋ฐ ์—ฌ๋ฒŒ์„ ๋ฐ›์•˜์œผ๋‹ˆ ์ถ”๊ฐ€
      }
    }
  }
  return n-arr.filter(function(v){ //๋ฐฐ์—ด์„ ๋Œ๋ฉด์„œ 
    return v===0 //value๊ฐ€ 0์ด๋ผ๋Š” ๊ฒƒ์€ ์—ฌ๋ฒŒ์„ ๋ชป๋ฐ›์•˜๋˜ ์• ๋“ค
  }).length // ๋ชป๋ฐ›์€ ์• ๋“ค์„ ์ˆ˜๋ฅผ ๋ฐ›์•„ ์ „์ฒด ํ•™์ƒ์—์„œ ๋นผ๊ธฐ
}
cs

'TIL' ์นดํ…Œ๊ณ ๋ฆฌ์˜ ๋‹ค๋ฅธ ๊ธ€

TIL 2020.11.13  (0) 2020.11.14
TIL 2020.11.12  (0) 2020.11.14
TIL 2020.11.11  (0) 2020.11.14
TIL 2020.11.09  (0) 2020.11.09
TIL 2020.11.08  (0) 2020.11.08

Eventhandlers

onsubmit

  • fires when the user submits a form

onmouseover

  • fires when the user moves the mouse over a particular element

event.preventDefault()

  • if the event does not get explicitly handled, its default action should not be taken as it normally would be

 

<textarea></textarea>

 

  • can assign how many rows and width
<textarea rows="5" cols="40" id="comment_input"></textarea>
  • allows user not to change size of textarea
textarea{
resize: none;
}

Local Storage

  • get items: brings string out from local storage
  • set items: save string inside local storage

reference: https://www.taniarascia.com/how-to-use-local-storage-with-javascript/

 

DOM

  • html์„ object๊ด€์ ์—์„œ ๋ณด๋Š” ๋ฐฉ๋ฒ•
  • DOM !== html
  • DOM !==JavaScript
  • document.body.(property)

 

String.prototype.trim()

  • removes whitespace from both ends of a string

 

arrayLike

  • An array-like or iterable object to convert to an array
  • can get index key and length but cannot use array property like forEach method
  • ex. string, object

 

Spread Syntax

  • to combine into array when the type is not an array
  • has to be iterable like array or arrayLike

 

QuerySelectorAll

  • cannot put eventlistener together
  • has to be per tag

 

tag.textContent or input.value

  • remembers when event happens and where to call that values
  • if you call in wrong spot, it will not show

'TIL' ์นดํ…Œ๊ณ ๋ฆฌ์˜ ๋‹ค๋ฅธ ๊ธ€

TIL 2020.11.14  (0) 2020.11.15
TIL 2020.11.12  (0) 2020.11.14
TIL 2020.11.11  (0) 2020.11.14
TIL 2020.11.09  (0) 2020.11.09
TIL 2020.11.08  (0) 2020.11.08

Where to add script in html

  • Once browser meets <script>tag, it stops interpreting html tag and operates JavaScript file first
  1. Add in <head>
  • If I put console.log in JavaScript file by using querySelector, I receive null
  • JavaScript file needs to bring info from html first in order to operate, but browser runs console log first before browser finishes reading html tags

2. Add in <body> right before it closes its tag

  • console.log gives what I am looking for so always put it this way!

 

Form Validation

Step 1. Compose UI

  • necessary step when we cooperate
  • shows all different status

:root

  • 1
    2
    3
    4
    5
    6
    7
    :root {
      --invalid-text-color: rgb(1775969);
      --invalid-border-color: rgba(1775969, .2);
      --invalid-background-color: rgba(2551922030.493);
      --valid-text-color: rgb(5117351);
      --minimum-width: 600px;
    }
    cs
  • allows you to set variable that you will use often so that you can save time to type continuously

display: none / display: block

  • allows you to set up css that will show up on certain time

Step 2. Create Function for form validation

  • return as Boolean since this function is only for valid/invalid
  • 1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    // [์œ ํšจ์„ฑ ๊ฒ€์ฆ ํ•จ์ˆ˜]: n๊ฐœ์˜ ๊ธ€์ž ์ด์ƒ
    function moreThanLength(str, n) {
      return str.length >= n;
    }
     
    // [์œ ํšจ์„ฑ ๊ฒ€์ฆ ํ•จ์ˆ˜]: ์˜์–ด ๋˜๋Š” ์ˆซ์ž๋งŒ ๊ฐ€๋Šฅ
    function onlyNumberAndEnglish(str) {
      return /^[A-Za-z][A-Za-z0-9]*$/.test(str);
    }
    // ^ beginning of string
    //[A-Za-z] from a-z capital or not
    //[A-Za-z0-9]* sequence with alphabet and number and sequence ends with *
    // $ end of string
    // / close expression
     
    // [์œ ํšจ์„ฑ ๊ฒ€์ฆ ํ•จ์ˆ˜]: ์ตœ์†Œ 8์ž ์ด์ƒํ•˜๋ฉด์„œ, ์•ŒํŒŒ๋ฒณ๊ณผ ์ˆซ์ž ๋ฐ ํŠน์ˆ˜๋ฌธ์ž(@$!%*#?&) ๋Š” ํ•˜๋‚˜ ์ด์ƒ ํฌํ•จ
    function strongPassword(str) {
      return /^(?=.*[A-Za-z])(?=.*\d)(?=.*[@$!%*#?&])[A-Za-z\d@$!%*#?&]{8,}$/.test(str);
    }
    // (?= ) matches a group after main expression w/o including in the result
    // . matches any character except line break
    // * quantifier, match 0 or more of preceding token
    // \d digit, matches  any digit from 0 to 9
    // {8,} match 8 or more of the precedig token
    cs

reference: https://stackoverflow.com/questions/19605150/regex-for-password-must-contain-at-least-eight-characters-at-least-one-number-a

Check phone number

Check identification number

Check visa or master card

  • 1
    2
    3
    4
    5
    6
    7
    function visaCard(str) {
      return /^(?:4[0-9]{12}(?:[0-9]{3})?)$/;
    }
     
    function masterCard(str) {
      return /^(?:5[1-5][0-9]{14})$/;
    }
    cs

reference: https://www.w3resource.com/javascript/form/credit-card-validation.php

Step 3. Connect Ul elements to event

  • use querySelector to call element form html and connect with eventhandler

onkeydown

  • fires when the user presses a keyboard key
  • onkeydown (๋ฒ„ํŠผ์ด ๋‚ด๋ ค๊ฐˆ ๋•Œ)-> onkeypress(๋ˆŒ๋ ธ์„ ๋•Œ)-> onkeyup(๋• ์„ ๋•Œ)

onkeyup

  • fires when the user releases a key that was previously pressed

onkeypress

  • should fire when the user presses a key on the keyboard. However, in practice browsers do not fire for certain keys, such as command keys.

onchange

  • fire when the user commits a value change to a form control
  • maybe good for password form validation…?

onclick

  • raised when the user clicks on an element

onmousedown

  • fires when the user depresses the mouse button

onmouseup

  • fires when the user releases the mouse button

Step 4. Create function for visual feedback

  • after form validation, you need to provide proper feedback to users

 

forEach

  • executes a provided function once for each array element.
  • returns undefined because action happens while it iterates
  • ํ•จ์ˆ˜ ๋ฆฌํ„ด ๊ฐ’์€ ํ•˜๋‚˜์˜ ๊ฐ’์ด์ง€๋งŒ, ์ˆœํšŒ๋ฅผ ๋Œ๋ฉด ์ ˆ๋Œ€ ํ•˜๋‚˜์˜ ๊ฐ’์ด ๋‚˜์˜ค์ง€ ์•Š์Œ. ๋‚˜์˜จ๋‹ค๋ฉด Map์ด๋ž‘ ๋™์ผํ•จ. forEach๋ฅผ ์‚ฌ์šฉํ•˜๋ฏ€๋กœ, ์›ํ•˜๋Š” ๊ธฐ๋Šฅ์„ ๊ฐ๊ฐ์˜ ๋ฐฐ์—ด์— ์ ์šฉ์‹œํ‚ด. ๋ฆฌํ„ด๊ฐ’์€ undefined์ด๋ฏ€๋กœ, ๋งŒ์•ฝ forEach๋กœ return๊ฐ’์„ ๊ตฌํ•˜๋ ค๊ณ  ํ•˜๋ฉด ์‹คํ–‰ ์•ˆ๋จ!!!!!!!!

 

EventTarget.addEventListener()

  • with this method, it creates event object
  • when you want to do something once clicked
  • when you want to use several functions with one button

'TIL' ์นดํ…Œ๊ณ ๋ฆฌ์˜ ๋‹ค๋ฅธ ๊ธ€

TIL 2020.11.14  (0) 2020.11.15
TIL 2020.11.13  (0) 2020.11.14
TIL 2020.11.11  (0) 2020.11.14
TIL 2020.11.09  (0) 2020.11.09
TIL 2020.11.08  (0) 2020.11.08

Rule of 72 Definition


JavaScript decimal places limit

toFixed( )

var num = 5.56789;
var n = num.toFixed(2); // 5.57

Recurrence Relation

  • allows you to find square root over Math.sqrt()


DOM(Document Object Model)

  • programming interface that can modify and manipulate html document like JavaScript Object
  • other languages has DOM as well
  • html code itself is not DOM but html code that you can open and manipulate in console log on browser is DOM
  • represents HTML as a tree structure of tags.
  • use ‘console.dir’ to show DOM
Node
  • the generic name for any type of object in the DOM hierarch
Element
  • one specific type of node and can be directly specified in the HTML with an HTML tag and can have properties like an id or class
  • node > element

Method

ParentNode.append()
  • inserts after the last child of the ParentNode.
  • append DOMString objects
  • has no return value
  • can append several nodes and strings
ParentNode.appendChild()
  • put tag inside tag and tag will be moved with its node well, not clone. Only accepts Node objects
  • only accepts Node objects.
  • returns the appended Node object.
  • can only append one node.
ParentNode.prepend():
  • similar to append but it will insert before the first child node
QuerySelectorAll()
  • returns all the elements
  • parentNode can be document and other tag as well as long as you call from html
QuerySelector()
  • returns the first element
ChildNode.remove()
  • only needs a reference to the child so making simpler to remove an element without having to look for the parent node

 

removeChild()
  • needs a reference both to the parent and the child
  • 1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    Remove Everything
    const container = document.querySelector('#container');
    while (container.firstChild) {
      container.removeChild(container.firstChild);
    }
     
    Remove with Restriction
    const container = document.querySelector('#container');
    while (container.children.length > 1) {
      container.removeChild(container.lastChild);
    }
    const tweets = document.querySelectorAll('.tweet')
    tweets.forEach(function(tweet){
        tweet.remove();
    })
    // or
    for (let tweet of tweets){
        tweet.remove()
    }
    cs

Properties

classList
  • add class

oneDiv.classList.add('tweet')

textContent

oneDiv.textContent = 'dev'

innerHTML

How to convert nodelist into javascript array

reference: https://gomakethings.com/converting-a-nodelist-to-an-array-with-vanilla-javascript/

'TIL' ์นดํ…Œ๊ณ ๋ฆฌ์˜ ๋‹ค๋ฅธ ๊ธ€

TIL 2020.11.13  (0) 2020.11.14
TIL 2020.11.12  (0) 2020.11.14
TIL 2020.11.09  (0) 2020.11.09
TIL 2020.11.08  (0) 2020.11.08
TIL 2020.11.07  (0) 2020.11.07

string.slice

  • https://developer.cdn.mozilla.net/pt-BR/docs/Web/JavaScript/Reference/Global_Objects/String/slice

INDENTION

  • use 2 space bar, not tab

Naming

  • Variable
    • Descriptive word
    • for finance, use its term
    • better to describe domain(๊ฐœ๋ฐœ ๋ถ„์•ผ)'s main point
    • let interestRate = 0.2
  • should reflect its meaning into varible name
  • Collletion
    • make plural
    • let animals = ['cat', 'dog', 'fish']
  • Boolean,
    • always make name with 'IS' and 'ARE'
    • isDog = true;
    • return right away if code evaluates by Boolean
      • return charSet.size > text.length;
  • Function
    • better to start with verb that can describe function well
    • let countWaterBlocksBetweenTowers ()
  • Class
    • start with capital
    • class Animal(){}
    • function Animal(){}
  • Constant
    • all 'CAPITAL'
    • const MAX_ITEMS_IN_QUEUE = 100

 Punctuation

  • do not omit Curly Bracket { }
    • for(key in object){
      alert(key);
      }
  • Single Quote ' '
    • can be double quote as well but do not mix
  • ` Backtick
    • to have it next line
    • do not use '\n'
    • let multilineTest = `this is line one
      this is line two
      this is line three`;
  • Semicolone
    • at the end of code
      • alert('hi');
    • no need for if, for, while statement
      • if (condition) {
        response();
        }
    • function expression
      • let greet = function(){
        alert('hi');
        };

Operator & Keyword

  • Loose Equality
    • ==
    • !=
  • Strict Equality
    • ===
    • !==
    • if (0 === '') {
      alert('looks like they\'re equal');
  • Ternary Operator(?)
    • for simple and readable
    • if (actual !== expected) {
      console.log('FAILED ' + testName + ': Expected ' + expected + ', but got ' + actual);
      } else {
      console.log('passed');
      }
  • not operator(!)
    • put it in front of variable
    • when it is clear such as when you make code with ||
    • if(!isEqual){}

Make it short

  • make code clear and short
  • function square(n){
    return n * n;
    }
  • Put one line space between codes
  • after comma, put 1 space
  • between operator, put 1 space

camelCase vs. snake_case

  • do not use snake case unless constant name, always CAMELCASE in JavaScript

 

reference: https://google.github.io/styleguide/jsguide.html

https://github.com/airbnb/javascript

https://ko.javascript.info/coding-style

nuli.navercorp.com/data/convention/NHN_Coding_Conventions_for_Markup_Languages.pdf

https://ui.toast.com/fe-guide/ko_CODING-CONVENTION/

First-class citizen(์ผ๊ธ‰ ๊ฐ์ฒด)

  • Function (ํ•จ์ˆ˜)
    1. can assign into variable
    2. can refer to argument(์ธ์ž) of other function
    3. can return as the result of other function
  • function expression(ํ•จ์ˆ˜ ํ‘œํ˜„์‹)
    • can not use before assignment(ํ• ๋‹น)
    • hoisting is not happening unlike 'function declaration'(ํ•จ์ˆ˜ ์„ ์–ธ์‹)

 

Higher order function(๊ณ ์ฐจ ํ•จ์ˆ˜)

ํ•จ์ˆ˜ = ๊ฐ’์„ ์ „๋‹ฌ ๋ฐ›์•„ ๊ฐ’์„ ๋ฆฌํ„ดํ•œ๋‹ค = ๊ฐ’์— ๋Œ€ํ•œ ๋ณต์žกํ•œ ๋กœ์ง์€ ๊ฐ์ถ”์–ด์ ธ ์žˆ๋‹ค = ๊ฐ’ ์ˆ˜์ค€์—์„œ์˜ ์ถ”์ƒํ™”

 

๊ฐ’ ์ˆ˜์ค€์˜ ์ถ”์ƒํ™”: ๋‹จ์ˆœํžˆ ๊ฐ’(value)์„ ์ „๋‹ฌ ๋ฐ›์•„ ์ฒ˜๋ฆฌํ•˜๋Š” ์ˆ˜์ค€

์‚ฌ๊ณ ์˜ ์ถ”์ƒํ™”: ํ•จ์ˆ˜(์‚ฌ๊ณ ์˜ ๋ฌถ์Œ)๋ฅผ ์ „๋‹ฌ ๋ฐ›์•„ ์ฒ˜๋ฆฌํ•˜๋Š” ์ˆ˜์ค€

 

๊ณ ์ฐจํ•จ์ˆ˜ = ํ•จ์ˆ˜๋ฅผ ์ „๋‹ฌ ๋ฐ›๊ฑฐ๋‚˜ ํ•จ์ˆ˜๋ฅผ ๋ฆฌํ„ดํ•œ๋‹ค = ์‚ฌ๊ณ (ํ•จ์ˆ˜)์— ๋Œ€ํ•œ ๋ณต์žกํ•œ ๋กœ์ง์€ ๊ฐ์ถ”์–ด์ ธ ์žˆ๋‹ค = ์‚ฌ๊ณ  ์ˆ˜์ค€์—์„œ์˜ ์ถ”์ƒํ™”

  • a function that takes as a argument or returns a function
  • Callback function
    • function passed into caller(another function) as an argument, which is then invoked(ํ˜ธ์ถœ)
    • depends on caller's condition, can decide callback function to be executed or executed even several times
    • mostly invoked when certain work is done so called as callback
  • Curry function
    • specifically for a function that returns function
    • part of higher order function
  1. When it takes caller(other function) as an argument
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    function triple(num) {
      return num * 3;
    }
     
    function doubleNum(func, num) {
      return func(num);
    }
     
    // ํ•จ์ˆ˜ doubleNum์€ ๋‹ค๋ฅธ ํ•จ์ˆ˜๋ฅผ ์ธ์ž๋กœ ๋ฐ›๋Š” ๊ณ ์ฐจ ํ•จ์ˆ˜์ž…๋‹ˆ๋‹ค.
    // ํ•จ์ˆ˜ doubleNum์˜ ์ฒซ ๋ฒˆ์งธ ์ธ์ž func์— ํ•จ์ˆ˜๊ฐ€ ๋“ค์–ด์˜ฌ ๊ฒฝ์šฐ
    // ํ•จ์ˆ˜ func๋Š” ํ•จ์ˆ˜ doubleNum์˜ ์ฝœ๋ฐฑ ํ•จ์ˆ˜์ž…๋‹ˆ๋‹ค.
    // ์•„๋ž˜์™€ ๊ฐ™์€ ๊ฒฝ์šฐ, ํ•จ์ˆ˜ double์€ ํ•จ์ˆ˜ doubleNum์˜ ์ฝœ๋ฐฑ ํ•จ์ˆ˜์ž…๋‹ˆ๋‹ค.
    let output = doubleNum(triple4);
    console.log(output); // -> 12
    cs
  2. When it returns function
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    function adder(added) {
      return function (num) {
        return num + added;
      };
    }
     
    // ํ•จ์ˆ˜ adder๋Š” ๋‹ค๋ฅธ ํ•จ์ˆ˜๋ฅผ ๋ฆฌํ„ดํ•˜๋Š” ๊ณ ์ฐจ ํ•จ์ˆ˜์ž…๋‹ˆ๋‹ค.
    // adder๋Š” ์ธ์ž ํ•œ ๊ฐœ๋ฅผ ์ž…๋ ฅ๋ฐ›์•„์„œ ํ•จ์ˆ˜(์ต๋ช… ํ•จ์ˆ˜)๋ฅผ ๋ฆฌํ„ดํ•ฉ๋‹ˆ๋‹ค.
    // ๋ฆฌํ„ด๋˜๋Š” ์ต๋ช… ํ•จ์ˆ˜๋Š” ์ธ์ž ํ•œ ๊ฐœ๋ฅผ ๋ฐ›์•„์„œ added์™€ ๋”ํ•œ ๊ฐ’์„ ๋ฆฌํ„ดํ•ฉ๋‹ˆ๋‹ค.
     
    // adder(5)๋Š” ํ•จ์ˆ˜์ด๋ฏ€๋กœ ํ•จ์ˆ˜ ํ˜ธ์ถœ ์—ฐ์‚ฐ์ž '()'๋ฅผ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.
    let output = adder(5)(3); // -> 8
    console.log(output); // -> 8
     
    // adder๊ฐ€ ๋ฆฌํ„ดํ•˜๋Š” ํ•จ์ˆ˜๋ฅผ ๋ณ€์ˆ˜์— ์ €์žฅํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.
    // javascript์—์„œ ํ•จ์ˆ˜๋Š” ์ผ๊ธ‰ ๊ฐ์ฒด์ด๊ธฐ ๋•Œ๋ฌธ์ž…๋‹ˆ๋‹ค.
    const add3 = adder(3);
    output = add3(2);
    console.log(output); // -> 5
    cs
  3. When it takes function as argument and returns function
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    function double(num) {
      return num * 2;
    }
     
    function doubleAdder(added, func) {
      const doubled = func(added);
      return function (num) {
        return num + doubled;
      };
    }
     
    // ํ•จ์ˆ˜ doubleAdder๋Š” ๊ณ ์ฐจ ํ•จ์ˆ˜์ž…๋‹ˆ๋‹ค.
    // ํ•จ์ˆ˜ doubleAdder์˜ ์ธ์ž func๋Š” ํ•จ์ˆ˜ doubleAdder์˜ ์ฝœ๋ฐฑ ํ•จ์ˆ˜ ์ž…๋‹ˆ๋‹ค.
    // ํ•จ์ˆ˜ double์€ ํ•จ์ˆ˜ doubleAdder์˜ ์ฝœ๋ฐฑ์œผ๋กœ ์ „๋‹ฌ๋˜์—ˆ์Šต๋‹ˆ๋‹ค.
     
    // doubleAdder(5, double)๋Š” ํ•จ์ˆ˜์ด๋ฏ€๋กœ ํ•จ์ˆ˜ ํ˜ธ์ถœ ๊ธฐํ˜ธ '()'๋ฅผ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.
    doubleAdder(5double)(3); // -> 13
     
    // doubleAdder๊ฐ€ ๋ฆฌํ„ดํ•˜๋Š” ํ•จ์ˆ˜๋ฅผ ๋ณ€์ˆ˜์— ์ €์žฅํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. (์ผ๊ธ‰ ๊ฐ์ฒด)
    const addTwice3 = doubleAdder(3double);
    addTwice3(2); // --> 8
    cs

 

Built-in higher order functions(๋‚ด์žฅ ๊ณ ์ฐจ ํ•จ์ˆ˜)

    • Array.prototype.filter() ๋ถ„๋ฅ˜ํ•ด์„œ ๊ฑฐ๋ฅด๊ธฐ!!
      • filter out with elements that satisfy certain condition from array
      • condition has to be delivered in function form as an argument of filter method
      • part of higher order function since function takes as an argument
      • returns back as an 'ARRAY' to callback function that takes argument
      • 'CALLBACK' function will return a Boolean value
        • If result is 'TRUE', it will create an array with that element, if not, NO
      • 1
        2
        3
        4
        5
        6
        7
        8
        9
        10
        11
        function getIndex(arr, num) {
          return arr.filter(function (el) {
            return el < num;
          }).length// make an array with true value and get the length of that array
        }
         
        function filterOddLengthWords(words) {
          return words.filter(function (word) {
            return word.length % 2 !== 0// create an array that is true 
          });
        cs
    • Array.prototype.map() ๋Œ€์‘, ๊ฐฏ์ˆ˜๋ฅผ ๋งž์ถฐ์ค˜์•ผ ๋จ ๋ฐฐ์—ด์ž์ฒด์˜ ์ˆ˜์ •์šฉ!!
      • creates a 'NEW ARRAY' populated with result of calling a provided function on every element in the calling array
      • can extract values from list of objects
        1
        2
        3
        4
        5
        function getOnlyNames(arr) {
          return arr.map(function (el){
            return el.name// extract value of name through map filter
          })
        }
        cs
    • Array.protoype.reduce() ๋ณ€ํ™˜์ด ๋˜์–ด ์‘์ถ•, ์—ฐ์‚ฐ์šฉ!!
      •  executes a reducer function(that you provide) on each element of the array, resulting in 'SINGLE OUTPUT VALUE'
      • can make various type
      • arr.reduce(callback[, initialValue])
        • ์ดˆ๊ธฐ๊ฐ’ ์„ค์ • ์ค‘์š”!
          • if they want empty array, put [] like this
        • if there is initial value, it becomes first value to calculate(A value to use as the first argument to the first call of the callback. If no initialValue is supplied, the first element in the array will be used as the initial accumulator value and skipped as currentValue.)
          1
          2
          3
          4
          5
          function computeProductOfAllElements(arr) {
            return arr.reduce(function(acc, cur){
              return acc * cur
            }, 1//1 is initial value
          }
          cs
      • use when you want to merge arrays by using concat
        • 1
          2
          3
          4
          5
          function joinArrayOfArrays(arr){
            return arr.reduce(function(acc, cur){
              return acc.concat(cur)
            })
          }
          cs
      • If array is empty and there is no initial value, it will return 'typeerror'
      • If array is empty and there is inital value, it will return initial value

  • Array.prototype.sort()
    • sorts the elements of an array in place and returns the 'SORTED ARRAY'
    • default sort order is ascending, built upon converting elements into string then comparing their sequences of UTF-16  code units values
    • a[key] > b[key]=> a-b=positive(3-1=2)
      • if it is true, return 1 which makes A to go to back
    • a[key] < b[key] => a-b=negative(1-3=-2)
      • if it is true, return -1, which makes B to go to back
    • a[key] = b[key] 
      • return 0, leave as it is
        1
        2
        3
        4
        5
        6
        7
        8
        9
        10
        items.sort(function (a, b) {
          if (a.value > b.value) {
            return 1;
          }
          if (a.value < b.value) {
            return -1;
          }
          // a must be equal to b
          return 0;
        });
        cs
  • Array.prototype.some()
    • tests whether 'AT LEAST ONE ELEMENT' in the array 'PASSES' the test implemented by the provided function
    • returns a Boolean Value
  • Array.prototype.every()
    • tests whether 'ALL ELEMENTS' in the array 'PASS' the test implemented by the provided function
    • returns a Boolean value
  • Array.prototype.forEach()
    • executes a provided function 'ONCE FOR EACH ARRAY ELEMENT'
  • Array.prototype.find()
    • returns the value of the 'FIRST ELEMENT' in the provided array that satisfies the provided testing function

'TIL' ์นดํ…Œ๊ณ ๋ฆฌ์˜ ๋‹ค๋ฅธ ๊ธ€

TIL 2020.11.12  (0) 2020.11.14
TIL 2020.11.11  (0) 2020.11.14
TIL 2020.11.08  (0) 2020.11.08
TIL 2020.11.07  (0) 2020.11.07
TIL 2020.11.06  (0) 2020.11.06

Spread syntax

  • if you want to add array inside middle of array
    1
    2
    const spread = [123];
    const arr = [0, ...spread, 4]; // [0, 1, 2, 3, 4])
    cs
  • if it is empty array, it will return nothing
    1
    2
    const spread = [];
    const arr = [0, ...spread, 4]; // [0, 4]
    cs
  • can merge arrays same as concat method
    1
    2
    3
    4
    const arr1 =[123];
    const arr2 = [345];
    const concatenated = [...arr1, ...arr2]; // [0, 1, 2, 3, 4, 5]
    arr1.concat(arr2); // [0, 1, 2, 3, 4, 5]
    cs
  • can merge objects as well
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    const paige = {
        firstName: 'Paige',
        LastName: 'Kim',
    };
    const me = {
        hobby: 'piltaes',
        todos: ['coplit''koans'],
    };
     
    const merged = {...paige, ...me};
    /* const merged = {
        firstName: 'Paige',
        LastName: 'Kim',
        hobby: 'piltaes',
        todos: ['coplit', 'koans'],
    };
    */
    cs
  • if spread syntax goes inside parameter, it become 'rest parameter(rest syntax)'
    • useful when numbers of parameters were not set
    • returns arguments with 'ARRAY'
      1
      2
      3
      4
      function pintMaxNums(...args) { 
          console.log(args); //[10, 30, 40]
      }
      pintMaxNums(103040); 
      cs

Object

  • === 
    • To compare 'REFERENCE' only in object!!
      1
      2
      3
      4
      5
      6
      7
      8
      9
      10
      11
      12
      const person = {
            son: {
              age: 9,
            },
          };
          
      const boy = person.son;
      boy.age = 20;
      expect(person.son.age).toBe(20);
      expect(person.son === boy).toBe(true);
      expect(person.son === { age: 9 }).toBe(false);
      expect(person.son === { age: 20 }).toBe(false);  
      cs

 

'TIL' ์นดํ…Œ๊ณ ๋ฆฌ์˜ ๋‹ค๋ฅธ ๊ธ€

TIL 2020.11.11  (0) 2020.11.14
TIL 2020.11.09  (0) 2020.11.09
TIL 2020.11.07  (0) 2020.11.07
TIL 2020.11.06  (0) 2020.11.06
TIL 2020.11.05  (0) 2020.11.05

Today, I reviewed all of the pair programming questions and the making of the calculator app with html, css, and JavaScript. My understanding is better compared to before when I first did it. I now have a better understanding of how I should write codes. And, also, with the help of my peers from last night, my terminal set up is done and super pretty!! ๐Ÿฅฐ

 

'TIL' ์นดํ…Œ๊ณ ๋ฆฌ์˜ ๋‹ค๋ฅธ ๊ธ€

TIL 2020.11.09  (0) 2020.11.09
TIL 2020.11.08  (0) 2020.11.08
TIL 2020.11.06  (0) 2020.11.06
TIL 2020.11.05  (0) 2020.11.05
TIL 2020.11.04  (0) 2020.11.04

It has already been two weeks since I started the program with Code States. When I first found out about this course, I wondered whether I would be able to get in. And if I ever do, will I even be able to finish the course? With determination and the willingness to change my career path, I decided to apply to the course and let fate pave the way. In order to apply, there were several essays I needed to write. The essays discussed who I am as a person and how much I am willing to learn. Maybe it has been more than 10 years since I last wrote an essay in Korean. It was definitely a good experience for me to try and at least considering the factor that I recently decided to settle down in Korea for the meantime. After hours and days of revising my essays and recording a video introduction about myself, I felt like I went back to senior year in high school and I was once again preparing essays for college. So, when I got an acceptance from code states, I felt like I was floating in the clouds. I was so happy that my effort and hard work paid off. Ever since I got the result, I set my mission and goal to be focused and work even harder than I ever did before. So that, at least, I can get some idea about how programming works. Truly, I will not take this opportunity for granted. I will work hard and not give up.

 

Code States provides recorded videos for us to first to learn the concepts and the foundation. They group us into pairs so that we can do pair programming to be able to review and learn what we watched from the recorded videos. I never had remote classes and online courses before so I was a bit worried whether I could follow the course well. But, it actually works for me because they give a detailed schedule on what to do. Moreover, I can post questions on git hub and they reply back as soon as they could (in less than thirty minutes). They are really helpful and they really guide us to become future programmers.

 

Working in pairs is really an eye-opening experience for me. At first, I did not fully understand what the driver does nor the functions of a navigator; so, in the beginning,  my partner and I worked without assigning a role for each other. But, on my third pairing, my pair gave me an idea about how each should operate. On a daily basis, after office hours, some of us joins zoom voluntarily in order to study more. This is another helpful practice because it really helps me out a lot. This is when I can see how working in a team and having roles assigned to each person works in the programming world. Different people have different learning styles and strengths, so gathering in a group and working together really brings out the value of teamwork and I can see how this will work out once we start being part of a team of different programmers. And, during the study group after official sessions, I was able to ask questions whenever I would get stuck at any point and all of them are willing to help each other out. I never thought of having a supportive and cheerful group who can study and communicate with each other for this course. I cannot wait to learn more and get to know more people. ๐Ÿคž๐Ÿป

HTML & CSS

1. To RESET html

1
2
3
4
5
6
7
8
{
  box-sizing: border-box;
}
 
body {
  margin: 0;
  padding: 0;
}
cs

 

2. FLEX: grow(ํŒฝ์ฐฝ ์ง€์ˆ˜), shrink(์ˆ˜์ถ• ์ง€์ˆ˜), basis(๊ธฐ๋ณธ ํฌ๊ธฐ) ๐Ÿคฏ

  • apply to children but does not give any property
  • decide how much you want to expand the children
  • flexbox can extend with need
    1
    flex: 0 1 auto;
    cs
  • grow
    • represents ratio
    • grow 1: expands equally
    • grow 0: only upto its content's size
      1
      2
      3
      .left {
        flex: 0 1 70px;
      }
       
  • shrink
    • leave as 1 most of times
    • need to study more how to count ๐Ÿคฏ
  • basis
    • if you give exact px, it will not grow more than that
    • if you put as '0%' over 'auto', each div will get exact same width regardless of length of each contents in div
  • if you put width and flex-basis together, flex-basis gets priority. If contents inside box overload, width does not guarantee exact size. In order to prevent that, it is better to use 'max-width' over 'width' if you do not use flex-basis.
  • In order to apply vertically, need to use as below,
    • put display:flex and flex-direction:column to parent and put flex on child
      1
      2
      3
      4
      5
      <div htmlclhtmlass="col w20">
          <div class="row h40">์˜์—ญ1</div>
          <div class="row h40">์˜์—ญ2</div>
          <div class="row h20">์˜์—ญ3</div>
      </div>
      cs
      1
      2
      3
      4
      5
      6
      7
      8
      .col{
          display: flex;
          flex-direction: column;
      }
       
      .h40{
          flex: 4 1 auto;
      }
      c

reference:

https://heropy.blog/2018/11/24/css-flexible-box/

https://studiomeal.com/archives/197

3. WIREFRAME

  • design layout to show how the final will be
  • first time making wireframe in order to start twittler with pair but not sure whether I did it correctly or not but it definitely helps me to brainstorm first before I write codes

  • Result with HTML & CSS:

'TIL' ์นดํ…Œ๊ณ ๋ฆฌ์˜ ๋‹ค๋ฅธ ๊ธ€

TIL 2020.11.08  (0) 2020.11.08
TIL 2020.11.07  (0) 2020.11.07
TIL 2020.11.05  (0) 2020.11.05
TIL 2020.11.04  (0) 2020.11.04
TIL 2020.11.03  (0) 2020.11.03

+ Recent posts