This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
function binarySearch(arr,number){ | |
// key at left side of the array | |
var min = 0; | |
var max = arr.length-1; | |
// Assigning a default value isn't necessary, let's assign it undefined | |
var mid = undefined | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
function linearSearch(arr,number){ | |
for(var i=0;i<arr.length;i++){ | |
if(arr[i]===number){ | |
// The required number is found, return it's index | |
return i; | |
} | |
} | |
/// Looping is complete, the number wasn't encountered in the supplied array, | |
// Return NOT_FOUND |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function shuffle(inputArr){ | |
for(var i=0;i<inputArr.length;i++){ | |
// Javascript way to implement a random number between a range | |
// The internals of how the random number is generated is not important | |
// in this scope, just know that the following line returns a number in a range | |
// random(min,max) | |
var randomIndex = Math.floor(Math.random() * (inputArr.length - i) + i) | |
// Swapping the numbers | |
var temp = inputArr[i] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function getNthSeriesInAP(a,d,n){ | |
/** | |
* a = first number in the AP | |
* n = Nth element of the series | |
* d = difference of the AP | |
*/ | |
return a+(n-1)*d | |
} | |
console.log((getNthSeriesInAP(0,2,101))) | |
/// => 200 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var seriesAP = [5,9,13,17,21,25,29,33,37,41] | |
function isAP(series){ | |
if(series.length<2){ | |
// not a valid series | |
} | |
// find the difference(d) of the series, if it is an arithmetic progression | |
// the difference of first two elements should suffice | |
var d = series[1]-series[0]; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
2 x 95 = 190 | |
2 x 96 = 192 | |
2 x 97 = 194 | |
2 x 98 = 196 | |
2 x 99 = 198 | |
2 x 100 = 200 | |
*/ | |
var N = 2; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
3 3 3 2 2 2 1 1 1 | |
3 3 2 2 1 1 | |
3 2 1 | |
*/ | |
var N=4 | |
for(var i=N;i>0;i--){ | |
// Every line that needs to be logged on every iteration of N | |
var line = "" | |
// Populating every line with numbers by iterating |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var Car = function(wheels, price, topSpeed){ | |
this.wheels = wheels; | |
this.price = price; | |
this.topSpeed = topSpeed; | |
} | |
function vroom(){ | |
console.log("Let's vroom with "+ this.wheels + " wheels on my "+ this.price +" $ car at "+this.topSpeed+" miles/hr"); | |
} | |
var newCar = new Car(4,40000,400) | |
vroom() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var Car = function(wheels, price, topSpeed){ | |
this.wheels = wheels; | |
this.price = price; | |
this.topSpeed = topSpeed; | |
} | |
function vroom(){ | |
console.log("Let's vroom with "+ this.wheels + " wheels on my "+ this.price +" $ car at "+this.topSpeed+" miles/hr"); | |
} | |
var newCar = new Car(4,40000,400) | |
vroom.call(newCar) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function printList(object1,object2,object3,object4,object5){ | |
console.log(object1+" "+object2+" "+object3+" "+object4+" "+object5); | |
console.log(this.misc); | |
} | |
var object = { | |
ListOfObjects:["element1","element2","element3","element4","element5"], | |
misc:"somethingElse" | |
} |
NewerOlder