Skip to content

Instantly share code, notes, and snippets.

View kotAPI's full-sized avatar
🎯
Focusing

Pranay Kothapalli kotAPI

🎯
Focusing
View GitHub Profile
//
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
//
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
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]
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
@kotAPI
kotAPI / isAP.js
Last active December 5, 2018 13:04
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];
/**
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;
@kotAPI
kotAPI / printpattern.js
Created December 3, 2018 09:33
Algorithm (Beginner)- Print the pattern - 332211
/*
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
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()
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)
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"
}