JS211 Week6

What I learned this week?

Sorting Algorithms: Bubble, Insertion, Merge and Quick Sort.

Higher order functions: .map(), .reduce() and .filter().

Javascripting.js

To make sure we solidify our understanding of programming and Javascript as a coding language you’re going to install Javascripting.js and work through each of the tasks in the program.

What are the pros and cons of immutability?

The immutability is a very highly exploited concept in functional programming languages, where the values of the variables do not change when assigned. This has the merit of simplifying the administration of the application state by reducing the number of shared variables.
As we will see, immutables objects render more efficient MVC frameworks such as React and AngularJS. This is also why the word immutability is more and more commonly understood in the Javascript community. In this article, we will see the pros and cons of using immutables data structures in Javascript code.

https://blog.invivoo.com/immutability-and-javascript/

How can you achieve immutability in your own code?

Going immutable

Instead of passing the object and mutating it, we will be better off creating a completely new object:

const person = {
name: 'John',
age: 28
}
const newPerson = Object.assign({}, person, {
age: 30
})
console.log(newPerson === person) // false
console.log(person) // { name: 'John', age: 28 }
console.log(newPerson) // { name: 'John', age: 30 }

Object.assign is an ES6 feature that takes objects as parameters. It will merge all objects you pass it into the first one. You are probably wondering why the first parameter is an empty object {}. If the first parameter would be ‘person’ we would still mutate person. If it would be { age: 30 }, we’d overwrite 30 with 28 again because that would be coming after. This solution works, we kept person intact, we treated it as immutable!

https://wecodetheweb.com/2016/02/12/immutable-javascript-using-es6-and-beyond/

What are Divide and Conquer algorithms? any common examples of the types of problems where this approach might be used? Discuss best, average, and worst case time and memory complexity.

  1. Divide: Break the given problem into subproblems of same type.
    2. Conquer: Recursively solve these subproblems
    3. Combine: Appropriately combine the answers

1) Binary Search is a searching algorithm. In each step, the algorithm compares the input element x with the value of the middle element in array. If the values match, return the index of middle. Otherwise, if x is less than the middle element, then the algorithm recurs for left side of middle element, else recurs for right side of middle element.

2) Quicksort is a sorting algorithm. The algorithm picks a pivot element, rearranges the array elements in such a way that all elements smaller than the picked pivot element move to left side of pivot, and all greater elements move to right side. Finally, the algorithm recursively sorts the subarrays on left and right of pivot element.

3) Merge Sort is also a sorting algorithm. The algorithm divides the array in two halves, recursively sorts them and finally merges the two sorted halves.

more about it blow of the link.

https://www.geeksforgeeks.org/divide-and-conquer-algorithm-introduction/

How do Insertion sort, and Heapsort, work?

Heap sort has the time complexity of a ‘divide and conquer’ algorithm (such as quick sort), but it does not behave like a divide and conquer algorithm.

Because it splits the data into a ‘sorted’ section and an ‘unsorted’ section, it is really a kind of selection sort. It takes advantage of the heap data structure to more efficiently select the minimum element from the unsorted list at each step. I suppose you could say it is a ‘priority queue sort’ because of this, but it should be mentioned that the entire operation can be done in-place rather than building a separate heap.

Heap sort is typically outperformed by quicksort, except that quicksort has a worst-case complexity of O(N^2) (versus heap sort's worst case of O(N.logN)).

Bubble sort is also an in-place algorithm, but it is considered ‘naive’.

Insertion sort is a reduce (decrease) and conquer algorithm. Decrease and conquer is a variant of divide and conquer. A given instance of a problem is reduced to a smaller instance of the same problem. This is smaller instance is solved and the solution then is extended to a solution of the original instance.

Insertion sort follows this scheme perfectly: You sort a sub-sequence that is shortened by one element and then insert the element into the sorted sub-sequence.

This can be done recursively top-down or bottom-up iteratively.

Explain the difference between mutable and immutable objects.

A mutable object is an object whose state can be modified after it is created. An immutable object is an object whose state cannot be modified after it is created. Examples of native JavaScript values that are immutable are numbers and strings. Examples of native JavaScript values that are mutable include objects, arrays, functions, classes, sets, and maps.

What is an example of an immutable object in JavaScript?

example your state was like this:

let state = {
'counter': 0,
'name': 'John',
'age': 36
}

If you continued the same example then your ADD reducer would need to return something like this

return {
'counter': state.counter+1,
'name': state.name,
'age': state.age
}

https://spapas.github.io/2018/04/05/easy-immutable-objects/

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Emi.K

Emi.K

Training at Austin Coding Academy to become. Web Developer!