EloquentJS - Data Structures

The sum of a range

Write a range function that takes two arguments, start and end, and returns an array containing all the numbers from start up to (and including) end.

Next, write a sum function that takes an array of numbers and returns the sum of these numbers. Run the previous program and see whether it does indeed return 55.

As a bonus assignment, modify your range function to take an optional third argument that indicates the “step” value used to build up the array.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
function range(start, end){
var arr = [];
while(start <= end){
arr.push(start);
start++;
}
return arr;
}

function sum(arr){
var sum = 0;
for(var i = 0; i < arr.length; i++){
sum +=arr[i]
}
return sum;
}

function range(start, end, step=1){
var arr = [];
if(end > start){
while(start <= end){
arr.push(start);
start += step;
}
} else if ( end < start ){
while(start >= end){
arr.push(start);
start += step;
}
} else {
arr = [start]
}
return arr;
}

Reversing an Array

Arrays have a method reverse, which changes the array by inverting the order in which its elements appear. For this exercise, write two functions, reverseArray and reverseArrayInPlace. The first, reverseArray, takes an array as argument and produces a new array that has the same elements in the inverse order. The second, reverseArrayInPlace, does what the reverse method does: it modifies the array given as argument in order to reverse its elements. Neither may use the standard reverse method.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
function reverseArray(arr){
var tempArr =[];
for(var i = arr.length-1; i >= 0;i--){
tempArr.push(arr[i]);
}
return tempArr;
}

function reverseArrayInPlace(someArray){
var middleValue = Math.floor(someArray.length/2);
var currentValue;
var lastValue;
var mirrorValue;
for (var i = 0; i <= middleValue; i++){
currentValue = someArray[i];
lastValue = someArray[someArray.length - 1 - i];
mirrorValue = currentValue;
someArray[i] = lastValue;
someArray[someArray.length - 1 - i] = mirrorValue;
}
return someArray
}

A List

A common data structure is the list (not to be confused with the array). A list is a nested set of objects, with the first object holding a reference to the second, the second to the third, and so on.

Write a function arrayToList that builds up a data structure like the previous one when given [1, 2, 3] as argument, and write a listToArray function that produces an array from a list

1
2
3
4
5
6
7
8
9
function arrayToList(arr){

var list = {};
for(var i = 0; i < arr.length; i++){
list.value = arr.splice(0,1)[0];
list.rest = (arr.length > 0 ) ? arrayToList(arr) :null;
}
return list;
}

Deep comparison

Write a function, deepEqual, that takes two values and returns true only if they are the same value or are objects with the same properties whose values are also equal when compared with a recursive call to deepEqual.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
var deepEqual = function(x, y){
if((typeof x == "object" && x != null) && (typeof y == "object" && y != null)){
var allPropertiesEqual = true;
for( var prop in x ){
if(y.hasOwnProperty(prop)){
allPropertiesEqual = deepEqual(x[prop], y[prop]) && allPropertiesEqual;
} else {
allPropertiesEqual = false;
}
return allPropertiesEqual;
}
} else {
return x ===y;
}
}

var obj = {here: {is: "an"}, object: 2};
console.log(deepEqual(obj, obj));
// → true
console.log(deepEqual(obj, {here: 1, object: 2}));
// → false
console.log(deepEqual(obj, {here: {is: "an"}, object: 2}));
// → true