• +234 8079216793
  • seunowonikoko@gmail.com
  • Google Mail
  • Yahoo Mail
  • Facebook
  • Instagram
  • Twitter
  • Github
  • Medium

Binary Search

The topic may say Binary Search, but this project is really about extending the array object in javascript. Click Binary Search, if you want to learn more about this search algorithm. I got this idea from a question i was given to solve:

Question: Implement the binary search algorithm with 3 arrays, populating the arrays as thus; array_1(1 -20, with intervals of 1), array_2(2 - 40, with intervals of 2), and array_3(10 -1000, with intervals of 10). Where you add a prototype method to the Array Object to generate these arrays

After writing the code, I realized the for-loops I had written to generate arrays I needed as input for my binary search test could be refactored. See below

/*

Array.prototype.gen_arrays(sv, mf);

Array.prototype.gen_arrays:

This is an extension of the array methods in javascript.

It can be used to generate one or more arrays. Emphasis

on more arrays, when these arrays to be generated have

their incremental difference (the difference between

consecutive values) as constant and also, the highest value

of each array has a common difference.

Advantages:

* Code Redability: it makes your code shorter and more readable

* Code Refactoring: This on its own has two advantages which are

Extensibility and Maintainability.

sv => Starting value of the array. It is also the incremental

difference between the generated array. The difference

between this generated array is constant.

mf => Multiplying factor of the array. This is the value when

multiplied with the common difference cd, will give you the

highest value of the generated array.

cd => Common difference between the highest value of the arrays

to be generated. The H.C.F. of the highest value of the arrays

to be generated.

For example:

1. gen_arrays(1 to 20)

2. gen_arrays(2 to 40)

3. gen_arrays(10 to 1000)

cd of (20, 40, 1000) = 20

For 1:

sv => 1

cd => 20

mf => 1

Therefore: the highest value of the array (20) === cd*mf (20)

For 2:

sv => 2

cd => 20

mf => 2

Therefore: the highest value of the array (40) === cd*mf (40)

For 3:

sv => 10

cd => 20

mf => 50

Therefore: the highest value of the array (20) === cd*mf

Note: The common difference (cd) is constant

The For Loop:

var demo_arr = [];

for(i=0; i<(mf * cd); i+=sv){

demo_arr.push(i + sv);

}

*/

//Where arrays to generate are [1 - 20], [2 - 40], [10 -1000], which makes cd = 20

//Code

Array.prototype.gen_arrays = function(sv, mf){

for(i=0; i

this.push(" " + (i + sv));

}

}

var one_to_twenty = [];

var two_to_forty = [];

var ten_to_thousand = [];

one_to_twenty.gen_arrays(1, 1);

two_to_forty.gen_arrays(2, 2);

ten_to_thousand.gen_arrays(10, 50);

//logs out generated arrays

console.log("----Generated Arrays----")

console.log("1-20: [" + one_to_twenty + " ]");

console.log("2-40: [" + two_to_forty + " ]");

console.log("10-1000: [" + ten_to_thousand + " ]");

//logs out the length of generated arrays

console.log("");

console.log("----Length of generated arrays----");

console.log("length of 1-20, multiples of 1: " + one_to_twenty.length);

console.log("length of 2-40, multiples of 2: " + two_to_forty.length);

console.log("length of 10-1000, multiples of 10: " + ten_to_thousand.length);

/*

Please feel free to contribute

Mail me: seunowonikoko@gmail.com

*/