🌌 Day 7 Algorithm Review

JBΒ·2022λ…„ 3μ›” 22일
0

Algorithms

λͺ©λ‘ 보기
8/12

🌚 Replit

str.split(", ")

const handleEdit = (nickname, interests) => {
  console.log(interests.split(", "))	// [ 'λ°©νƒˆμΆœ', 'ν…Œλ‹ˆμŠ€', '멍 λ•Œλ¦¬κΈ°' ]
  const handleObj = {
    nickname: nickname,
    interests: interests.split(', '),
    bio: `제 λ‹‰λ„€μž„μ€ ${nickname}μž…λ‹ˆλ‹€. μ·¨λ―ΈλŠ” ${interests.split(', ')}μž…λ‹ˆλ‹€.`
  }
  return handleObj
} 

handleEdit('λšœλΉ„', 'λ°©νƒˆμΆœ, ν…Œλ‹ˆμŠ€, 멍 λ•Œλ¦¬κΈ°')
// {
//  nickname: 'λšœλΉ„',
//  interests: [ 'λ°©νƒˆμΆœ', 'ν…Œλ‹ˆμŠ€', '멍 λ•Œλ¦¬κΈ°' ],
//  bio: '제 λ‹‰λ„€μž„μ€ λšœλΉ„μž…λ‹ˆλ‹€. μ·¨λ―ΈλŠ” λ°©νƒˆμΆœ,ν…Œλ‹ˆμŠ€,멍 λ•Œλ¦¬κΈ°μž…λ‹ˆλ‹€.'
// }

str.trim()

const example = " asdf asdf "
example.trim() // Remove both first and last indent
example.trimStart()	// Remove first indent
example.trimEnd() // Remove last indent

🌝 Class

isNaN vs. Number.isNaN

// isNaN
// 1. ν•΄λ‹Ή 데이터가 NaN 값을 κ°€μ§€λŠ”μ§€ νŒλ‹¨
// 2. 좜λ ₯값이 false일 λ•ŒλŠ” μˆ«μžκ°€ λ§žλ‹€. true일 λ•ŒλŠ” NaN값을 가진닀

isNaN("a")	//true
isNaN("10")	//false

// Number.isNaN
// 1. ν•΄λ‹Ή 데이터가 Number νƒ€μž…μ΄λ©΄μ„œ NaN 값이 λ§žλŠ”μ§€λ₯Ό 검증
// 2. NaN 검증을 μ’€ 더 μ—„κ²©ν•˜κ²Œ 진행

isNaN("0/0")	//true
Number.isNaN("0/0")	//false

isNaN(undefined) //true
Number.isNaN(undefined) //false

πŸƒ λ¬Έμžμ—΄ 닀루기 κΈ°λ³Έ

function solution(s){
  if (s.length !== 4 && s.length !== 6){
    return false
  }
  
const answer = s.split("").filter(num => {
  // λ¬Έμžκ°€ λ§žλŠ” λ°μ΄ν„°λ§Œ 남기기
  return isNaN(num)
})

// 배열이 λΉ„μ–΄μžˆλŠ”μ§€ (즉 λ¬Έμžμ—΄μ΄ ν•˜λ‚˜λΌλ„ μ‘΄μž¬ν•˜λŠ”μ§€)λ₯Ό νŒλ‹¨ν•΄μ„œ
// 배열이 λΉ„μ–΄μžˆμ§€ μ•Šλ‹€λ©΄ false, λΉ„μ–΄μžˆλ‹€λ©΄ Trueλ₯Ό 리턴
return answer.length ===0
}

solution("a234") //false
solution("8ys2") //false
solution("1234") //true

πŸƒ μ•½μˆ˜μ˜ ν•©

function solution(n){
    let answer = 0;
    for (let i = 1; i<=n; i++){
      if (n%i===0){
        answer += i
      }
    }
    return answer
  }
//reduce method μ‚¬μš©
function solution(n){
  const answer = new Array(n).fill(1)
  									.reduce((acc, cur, i) => {

                      return n%(cur+i)===0 
                      ? acc + (cur + i) 
                      : acc
  									},0)
  
  console.log(answer)
}

solution(12) // 28
solution(5)  // 6
profile
두비두λ°₯λ°₯

0개의 λŒ“κΈ€