apress / js-data-structures-and-algorithms Goto Github PK
View Code? Open in Web Editor NEWSource code for 'JavaScript Data Structures and Algorithms' by Sammie Bae
Home Page: http://www.apress.com/9781484239872
License: Other
Source code for 'JavaScript Data Structures and Algorithms' by Sammie Bae
Home Page: http://www.apress.com/9781484239872
License: Other
Global Declaration: Global Scope
"In JavaScript, variables can be declared without using any operators"
The above statement is correct saying only about the "non-strict" mode. In the strict mode "use strict"
would cause a ReferenceError.
Declaration with let: Block Scope
function scope3(print) {
if(print) {
let insideIf = '12';
}
console.log(insideIf)
}
scope3(true);
In the snippet above there would be a ReferenceError
because as the title says let (and const) are Block Scoped variables.
Truthy/Falsy Check
"Here are commonly used expressions that evaluate to true:"
- true
- Any number other than 0
- Non-empty strings
- Non-empty object
Objects in JavaScript even if it's empty will always give the truthy result. Hence the last statement in the list is not correct.
I suppose it would be better to say:object
(any object)
Man, you literally saved me like a hundreds of hours to figure out what is wrong. Many thanks
Originally posted by @Katarinich in Apress/roguelike-development-javascript#3 (comment)
if (this.rightChild(index) && this.rightChild(index) < this.items[smallerIndex]) {
// if right is smaller, right swaps
smallerIndex = this.rightChildrenIndex(index);
}
rightChildrenIndex
doesn't exist I think you meant rightChildIndex
EDIT: I realized this is a mistake on my part. rightChildrenIndex
is defined and rightChildIndex
is not. It was a little confusing when typing up the implementation because the left child is called leftChildIndex
and leftChild
and the right side node getter is rightChild
so it made sense for the right side index getter to be rightChildIndex
function someFunction(n) {
for (var i = 0; i < n; i * 2) {
console.log(n);
}
}
'answer: O(log_2n)';
i * 2, when i=0, always gives 0, so this is an endless loop, not O(log_2n) O(log_2n)
Earlier in chapter 5 about Arrays, it was mentioned that passing no argument to the slice
function returns a copy of the array. So if we tried to do:
var array = [1, 2, 3];
console.log(array.slice() === array); // this will return false
While in your example here you are returning the array itself so:
var array = [1, 2, 3];
console.log(arraySlice(array) === array); // this will return true
I think it should be replaced by:
if (!beginIndex && !endIndex) {
return Array.from(array);
}
for example if
var M = [
[1, 2, 3],
[4, 5, 6],
[7, 8, 9]
];
it stops printing at 8 and skips end of spiral 7, 4, 5
Hello, I don't understand why the inner loop has to start at i+1 what if the number we're trying to find (weight) is 8?
Then 4+4 would work. Thus we should loop through everything again in the inner loop no? More details are required.
Push function must be defined in advance
$ node Chapter12-StackQueue.js
Stack { array: [] }
/Users/Documents/js-data-structures-and-algorithms/Chapter12-StackQueue.js:22
stack1.push(10);
^
TypeError: stack1.push is not a function
at Object.<anonymous> (/Users/Documents/js-data-structures-and-algorithms/Chapter12-StackQueue.js:22:8)
at Module._compile (internal/modules/cjs/loader.js:956:30)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:973:10)
at Module.load (internal/modules/cjs/loader.js:812:32)
at Function.Module._load (internal/modules/cjs/loader.js:724:14)
at Function.Module.runMain (internal/modules/cjs/loader.js:1025:10)
at internal/main/run_main_module.js:17:11
Spread Operator
It says:
Both the Math.max and Math.min functions take an unlimited number of parameters, so you can use the spread operator for the following operations.
To find the maximum in an array, use this:
and the snippet with a type goes below:
var array1 = [1,2,3,4,5];
Math.max(array1); // 5
var array2 = [3,2,-123,2132,12];
Math.min(array2); // -123
I suppose it should be:
var array1 = [1,2,3,4,5];
Math.max(...array1); // 5
var array2 = [3,2,-123,2132,12];
Math.min(...array2); // -123
Exercises
FIND TWO ARRAY ELEMENTS IN AN ARRAY THAT ADD UP TO A NUMBER
problem: Given the array arr, find and return two indices of the array that add up to weight or return -1 if there is no combination that adds up to weight.
In the code snippet below there is a typo: return [i, hashtable[weight - currentElement]];
function findSumBetter(arr, weight) {
var hashtable = {};
for (var i = 0, arrLength = arr.length; i < arrLength; i++) {
var currentElement = arr[i],
difference = weight - currentElement;
// check the right one already exists
if (hashtable[currentElement] != undefined) {
return [i, hashtable[weight - currentElement]];
} else {
// store index
hashtable[difference] = i;
}
}
return -1;
}
I suppose it should be like this: return [i, hashtable[currentElement]];
. You should return the index of the previously found match from the hashtable
In chapter 15 when talking about AVLTree you call updateInNewLocation and getDepthFromChildren but it's never defined in the book?
CONVERT DECIMAL (BASE 10) TO BINARY NUMBER - should be convert INTEGER.
Not a big deal but in chapter 16 the item
arg isn't needed
Heap.prototype.peek = function(item) {
return this.items[0];
}
function primeFactors(n){
// Print the number of 2s that divide n
while (n % 2 == 0) {
console.log(2);
n = n/2;
}
// n must be odd at this point. So we can skip one element
// (Note i = i +2)
for (var i = 3; i * i <= n; i = i + 2) {
// While i divides n, print i and divide n
while (n % i == 0) {
console.log(i);
n = n / i;
}
}
// This condition is to handle the case when n is a prime number
// greater than 2
if (n > 2) {
console.log(n);
}
}
This function has Big-O =
In your book you put it sqrt(x) only, but inside of the for
loop there is another loop while
, that has Big-O = log (x). Am I correct?
BinarySearchTree.prototype.remove fails to remove parent node in the following example.
example
var bst1 = new BinarySearchTree();
bst1.insert(1);
bst1.insert(3);
bst1.insert(2);
console.log(bst1.findNode(3)); // true
console.log(bst1.findNode(5)); // false
bst1.remove(1);
bst1.remove(3);
bst1.remove(2);
console.log(bst1.findNode(1)); // true <-- should be false
In chapter 16 the MinHeap.bubbleDown()
has two different implementations. When first shown it has
while (this.leftChild(index) && this.leftChild(index) < this.items[index])
But in the full implementation it also checks rightChild
which the original does not
while (this.leftChild(index) && this.leftChild(index) < this.items[index] || this.rightChild(index) < this.items[index])
I also wonder if rightChild
needs to be checked like leftChild
is
The write-up under Rotate Left seems not to be talking about Rotate Left.
Then, at Rotate Right, the write-up is the same. One can infer that at least one of the two write-ups is wrong.
Likewise, the methods that follow are not clear as they do not follow the preceding write ups. Perhaps for each line, if you printed out the output, one can see how everything comes together
For example
var valueBefore = this.value; // please print out
var rightBefore = this.right; // please print out
You wrote in the book (page 21) that JavaScript numbers are 32 bit, while in all sources and specs you will find that it is in fact 64 bit.
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.