Skip to content

Instantly share code, notes, and snippets.

View sawant's full-sized avatar
🏠
Working from home

Sawant Shah sawant

🏠
Working from home
View GitHub Profile
@sawant
sawant / 4-4-deepcomparison.js
Created September 2, 2014 14:01
The == operator compares objects by identity. But sometimes, you would prefer to compare the values of their actual properties. 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. T…
/*
http://eloquentjavascript.net/04_data.html
The == operator compares objects by identity. But sometimes, you would prefer to compare the values of their actual properties.
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.
To find out whether to compare two things by identity (use the === operator for that) or by looking at their properties, you can use the typeof operator. If it produces "object" for both values, you should do a deep comparison. But you have to take one silly exception into account: by a historical accident, typeof null also produces "object".
*/
@sawant
sawant / 4-3-alist.js
Created September 2, 2014 13:26
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. Also write the helper functions prepend, which takes an element and a list and creates a new list that adds the element to the front of the input list, and nth, …
/*
http://eloquentjavascript.net/04_data.html
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. Also write the helper functions prepend, which takes an element and a list and creates a new list that adds the element to the front of the input list, and nth, which takes a list and a number and returns the element at the given position in the list, or undefined when there is no such element.
If you haven’t already, also write a recursive version of nth.
*/
function arrayToList( array ) {
var list = null;
@sawant
sawant / 4-2-reversearray.js
Created September 2, 2014 12:33
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, doe…
/*
http://eloquentjavascript.net/04_data.html
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.
*/
function reverseArray( array ) {
var reversedArray = [];
while( i = array.pop() )
/*
http://eloquentjavascript.net/04_data.html
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. If no step is given, the array elements go up by increments of one, corresponding to the old behavior. The function call range(1, 10, 2) should return [1, 3, 5, 7, 9]. Make sure it also works with negative step values so that range(5, 2, -1) produces [5, 4, 3, 2].
*/
@sawant
sawant / iseven.js
Created August 27, 2014 16:40
3.2 Recursion
/*
We’ve seen that % (the remainder operator) can be used to test whether a number is even or odd by using % 2 to check whether it’s divisible by two. Here’s another way to define whether a positive whole number is even or odd:
Zero is even.
One is odd.
For any other number N, its evenness is the same as N - 2.
Define a recursive function isEven corresponding to this description. The function should accept a number parameter and return a Boolean.
@sawant
sawant / power.js
Created August 27, 2014 16:00
JavaScript Power Recursive
var power = function(base, exp) {
if (exp == 1)
return base;
else
return base * power(base, exp-1);
}
console.log(power(2, 7));
@sawant
sawant / ascii-triangle.js
Created August 27, 2014 11:55
JavaScript ASCII Right Triangle
/*
http://eloquentjavascript.net/02_program_structure.html
Write a loop that makes seven calls to console.log to output the following triangle:
#
##
###
####
#####
@sawant
sawant / chess-ascii.js
Last active July 27, 2023 17:50
JavaScript Chess Board
/*
http://eloquentjavascript.net/02_program_structure.html
Write a program that creates a string that represents an 8×8 grid, using newline characters to separate lines. At each position of the grid there is either a space or a “#” character. The characters should form a chess board.
Passing this string to console.log should show something like this:
# # # #
# # # #
# # # #
@sawant
sawant / Pakistan Internet Censorship - Feb 2014.md
Last active August 29, 2015 13:55
Previously operational websites and online services, which seem to have stopped working in Pakistan since 1st Feb 2014.

New Wave of Inaccessible/Blocked websites and services

A number of websites and online services, which were previously operational, have stopped working in Pakistan since 1st Feb (verified on PTCL, Cybernet and Multinet, in Karachi). The sites appear broken and their services, if any, are not working as intended. This could be a temporary glitch, or it could be a side-effect of the Pakistani government/PTA banning websites and IP addresses.

Following are the sites/services I have come across that are not working entirely or partially (report any other sites/services in the comments or tweet them to @sawant):

List of Inaccessible / Blocked Websites

@sawant
sawant / Pakistan Internet Censorship.md
Last active August 29, 2015 13:55
A number of websites and online services, which were previously operational, have stopped working in Pakistan since 1st Feb.

A number of websites and online services, which were previously operational, have stopped working in Pakistan since 1st Feb (verified on PTCL and Cybernet, in Karachi). The sites appear broken and their services, if any, are not working as intended. This could be a temporary glitch, or it could be a side-effect of the Pakistani government/PTA banning websites and IP addresses.

Following are the sites/services I have come across that are not working entirely or partially from Karachi, Pakistan (if you come across any others, tweet them to @sawant):