Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //call stack
- //recursion
- //reduce
- //stack data structure
- // coin stack
- // ball stack
- //LIFO (Last In First out)
- //Ball 4
- //Ball 3
- //Ball 2
- //Ball 1
- //Queue (data structure )
- //(cash counter) 1 - 2 - 3 -4 - 5 - 6
- // First In First Out(FIFo)
- function three() {
- console.log(3)
- }
- function two() {
- three()
- console.log(2)
- }
- function one() {
- two()
- console.log(1)
- }
- one()
- //factorial 5! = 5 * 4 * 3 * 2 * 1
- //0! = 1
- //recursion
- // function factorial(num) {
- // let result = 1
- // if (num === 0) {
- // return result
- // }
- // for (let i = num; i >= 1; i--) {
- // result *= i
- // console.log(i)
- // }
- // console.log(result)
- // }
- // console.log(factorial(10))
- // 5 * 4 * 3 * 2 * 1
- // function factorial(num) {
- // //ending condition (base case)
- // if (num === 0) {
- // return 1
- // }
- // console.log(num)
- // return num * factorial(num - 1)
- // }
- // console.log(factorial(5))
- // 5 *factorial(4)
- // 4 * fact(3)
- //1 * fact(1)
- //1 * 1 * 2 * 3 * 4 * 5 = 120
- //5 * 4 * 3 * 2 * 1 * 1 = 120
- // sum of all values
- //reduce (mandatory)
- //1 + 2 = 3
- //3 + 3 = 6
- //6 + 4 = 10
- //10 + 6 = 16
- //10 + 1 = 11
- //11 + 2 = 13
- //13 + 3 = 16
- //16 + 4 = 20
- //20 + 6 = 26
- // function sum(arr) {
- // return arr.reduce((acc, currentNum) => {
- // console.log(acc)
- // console.log(currentNum)
- // return (acc += currentNum)
- // }, 10)
- // }
- // console.log(sum([1, 2, 3, 4, 6]))
- // let arr = [{ x: 1 }, { x: 2 }, { x: 3 }]
- // function sum(inputArr) {
- // return inputArr.reduce(
- // (acc, currentValue) => {
- // console.log(currentValue)
- // acc.total += currentValue.x
- // console.log(acc)
- // return acc
- // },
- // {
- // total: 0,
- // }
- // )
- // }
- // console.log(sum(arr))
- // let flattened = [
- // [0, 1],
- // [2, 3],
- // [4, 5],
- // ].reduce(function (previousValue, currentValue) {
- // console.log(previousValue, currentValue)
- // return previousValue.concat(currentValue)
- // }, [])
- // let names = ['Alice', 'Bob', 'Tiff', 'Bruce', 'Alice']
- // let countedNames = names.reduce(function (allNames, name) {
- // // console.log(allNames)
- // // console.log(name)
- // if (name in allNames) {
- // allNames[name]++
- // } else {
- // allNames[name] = 1
- // }
- // return allNames
- // }, {})
- // console.log(countedNames)
- // {
- // 'Alice': 1
- // }
- // countedNames is:
- // { 'Alice': 2, 'Bob': 1, 'Tiff': 1, 'Bruce': 1 }
- let people = [
- { name: 'Alice', age: 21 },
- { name: 'Max', age: 20 },
- { name: 'Jane', age: 20 },
- ]
- function groupBy(objectArray, property) {
- return objectArray.reduce(function (acc, obj) {
- console.log(obj)
- let key = obj[property]
- if (key in acc) {
- acc[key].push(obj)
- } else {
- acc[key] = [obj]
- }
- // console.log(acc)
- return acc
- }, {})
- }
- let groupedPeople = groupBy(people, 'age')
- console.log(groupedPeople)
- // groupedPeople is:
- // {
- // 20: [
- // { name: 'Max', age: 20 },
- // { name: 'Jane', age: 20 }
- // ],
- // 21: [{ name: 'Alice', age: 21 }]
- // }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement