๐ŸŒŒ Day 7 Algorithm Review

๐ŸŒš 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

์ข‹์€ ์›นํŽ˜์ด์ง€ ์ฆ๊ฒจ์ฐพ๊ธฐ