Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //Difference between maximum and minimum number
- // sorting
- //count occurrence
- // factorial and recursion
- //must use rest operator to receive the parameters
- // function differMaxMin(...numArr) {
- //solution-1 simple loop
- // let max_num = numArr[0]
- // let min_num = numArr[0]
- // for (let i = 0; i < numArr.length; i++) {
- // if (numArr[i] > max_num) {
- // max_num = numArr[i]
- // }
- // if (numArr[i] < min_num) {
- // min_num = numArr[i]
- // }
- // }
- // console.log(max_num - min_num)
- //Math.max, Math.min
- // const result = Math.max(...numArr) - Math.min(...numArr)
- // const result = Math.max.apply(null, numArr) - Math.min.apply(null, numArr)
- //console.log(result)
- //reduce
- //solution-3
- // const result1 = numArr.reduce((a, b) => {
- // return a > b ? a: b
- // })
- // const result2 = numArr.reduce((a, b) => {
- // return a < b ? a : b
- // })
- // console.log(result1- result2)
- //solution-4 (sort)
- // const sortedArr = numArr.sort((a, b) => a - b)
- // const max_num = sortedArr[sortedArr.length - 1]
- // const min_num = sortedArr[0]
- // console.log(max_num - min_num)
- // }
- // b-a (decending)
- // a - b (ascending)
- // 4 - 2 = 2
- // 2, 4 ,
- // 2, 4
- // differMaxMin(10, 2, 3, 4) //4-1 = 3
- //factorial and recursion 4! = 4 * 3 * 2 * 1
- // 4 * (4 - 1) * (3 - 1) * (2 - 1) * 1
- //recursion (calling itself)
- // 4 * 3 * 2 * 1 = 24
- // function factorial(number) {
- // if (number <= 1) {
- // return 1
- // }
- //solution-2
- // return number * factorial(number - 1)
- //return result(factorial)
- //solution - 1 (simple loop)
- // let result = 1
- // for(let i = 1; i <= number; i++ ){
- // console.log(result, i)
- // result *= i
- // }
- // for (let i = number; i > 0; i--) {
- // console.log(result, i)
- // result
- // result *= i
- // }
- // console.log(result)
- // }
- // console.log(factorial(24))
- function countOccurrence(numArr) {
- //solution -1 (plain way)
- // const obj = {}
- // for(let num of numArr){
- // if(num in obj){
- // obj[num]++
- // }else{
- // obj[num] = 1
- // }
- // console.log(num)
- // }
- // console.log(obj)
- //solution-2(Reduce)
- // return numArr.reduce((obj, currentNum) => {
- // obj[currentNum] ? obj[currentNum]++ : (obj[currentNum] = 1)
- // return obj
- // }, {})
- }
- // console.log(countOccurrence([1, 1, 2, 2, 3, 5]))
- // hash map (hashing){
- // '1': 2,
- // '2': 2,
- // '3' : 1,
- // '5': 1
- // }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement