항해14기 온보딩/항해14기 온보딩 과제

[항해14기] 온보딩 과제4 2023/03/09

스쿼트잘함 2023. 3. 10. 01:39

1. Chapter 5-1

1)

<script>
  function multiplyAll(a,b) {
    let output = 1
    for (i=a; i<=b; i++) {
      output *= i
    }
  return output
  }
  console.log(multiplyAll(1,2))
  console.log(multiplyAll(1,3))
</script>

2-1)

<script>
  const max = function max(array) {
    let output = array[0]
    for  (const item of array) {
    if (output < item) {
    output = item
    }
 }
    return output
  }

  console.log(max([1,2,3,4]))
</script>

2-2)

<script>
  const max = function max(...array) {
    let output = array[0]
    for (const item of array) {
     if (output < item) {
     output = item
       }
    }
        return output
  }
  console.log(max(1,2,3,4))
</script>

2-3)

<script>
  const max = function max( a, ...b) {
    let output
    let items
    
    if (Array.isArray(a)) {
      output = a[0]
      items = a
    } else if (typeof(a) === 'number') {
      output = a
      items = b
    }

    for (const item of items) {
    if (output < item) {
      output = item
      }
    }
    return output
  }
  console.log(`max(배열) : ${max([1,2,3,4])}`)
  console.log(`max(숫자,...) : ${max(1,2,3,4)}`)
</script>

 

2. Chapter 5-2

1)

<script>
  let numbers = [273,25,75,52,103,32,57,24,76]

  numbers = numbers.filter(function (value) {
    return value % 2 === 1
  })

  console.log(numbers)
</script>
<script>
  let numbers = [273,25,75,52,103,32,57,24,76]

  numbers = numbers.filter(function (value) {
    return value <= 100
  })

  console.log(numbers)
</script>
<script>
  let numbers = [273,25,75,52,103,32,57,24,76]

  numbers = numbers.filter(function (value) {
    return value % 5 === 0
  })

  console.log(numbers)
</script>

 

2)

<script>
  const array = ['사과','배','귤','바나나']

  console.log('# for in 반복문')
  for (const i in array) {
    console.log(i)
  }

  console.log('#for of 반복문')
  for (const i of array) {
    console.log(i)
  }
</script>
<script>
  const array = ['사과', '배', '귤','바나나']

  array.forEach(function (value, index, array) {
    console.log(`${index}`)
  })
  array.forEach(function (value, index, array) {
    console.log(`${value}`)
  })
</script>