Process of writing simple Algorithm | Z Data Tech

Process of writing simple Algorithm

by
Maybe you happen to come across a chapter about algorithms in a book titled "Programming Pearls". In one of the chapters you notice basic pseudocode trying to solve a problem by coding a simple algorithm. The simple algorithm is being used to select a range of numbers between two largest numbers in a list and provide a sum. For the example "Programming Pearls" uses a list of numbers. The pseudocode is in C language syntax and you wonder how complex would it be to write this algorithm in JavaScript that actually worked. You place sample list of numbers in JavaScript using Array syntax. In JavaScript, Arrays are created using brackets. You assign the array of numbers from "Programming Pearls" book to a variable xvector.



   var xvector = [31,-41,59,26,-53,58,97,-93,-23,84];
After declaring the above variable you "let the pot boil for a while" as they say. Most languages have pre-built functions, you wonder, maybe JavaScript also has pre-built functions. Where would those functions be listed? You visit your local bookstore and notice "JavaScript the definitive guide". Hey, there is a list of all or at least the most used pre-built functions in the language. You find there is a Math function with the option of max to find max values when providing an argument that is a string. After a few console.log errors, you test out proper string argument required by Math.max function. You discover it's best to use apply method shown in "The definitive guide".



   Math.max.apply(null, your-argument);
You start writing down the variables and constants you need for this algorithm to avoid later confusion when debugging your code. You also realize you have to store the position of each number in the list.



   var positionOne = 0;
   var positionTwo = 0;
Also you figure, amount of numbers provided to algorithm needs to be divided in half, to later find the largest number in each half.



   halfOfVector = xvector.length / 2; 
Algorithm also needs to temporarily hold the value of two largest numbers.



    var largeOne = 0;
    var largeTwo = 0;
Everytime the algorithm script runs the algorithm script needs to reset the value of halfOfVector.



    var halfOfVector = 0;
Let's also reset the totalMax value to zero.

  

    var totalMax = 0;
We will traverse through half of the numbers in vector using i and other half using j.



     var i = [];
     var j = [];
After writing first "for loop" to traverse through first half of numbers. How do you make JavaScript interpreter traverse a list of numbers in reverse? You leave this pot on low heat and step away to ponder. Maybe spinning a fidget spinner, throwing magnetic darts on a board, or doing your daily chores. You decide to store the amount of numbers in list as reverseCount and minus reverseCount with each traversal in for loop.



  var reverseCount = xvector.length - 1;
While you are writing your "for loops" you are also writing out console.log to see if the end result is what you want to return. You wrap the entire script in a function that accepts a list of numbers.



   findLargestNum(xvector) { .. };
You also include a set of console.log to verify your work.



console.log("totalMax from position "+positionOne+" "+xvector[positionOne]+" to  "+xvector[positionTwo]+" "+ positionTwo +" seems to be "+totalMax+"");
console.log("large x :"+largeOne+"");
console.log("large y :"+largeTwo+"");
console.log("complete vertex "+ xvector +"");

Complete HTML document with Simple Algorithm Code




<!DOCTYPE html>
<html lang="en">
 <head>
    <title>test in browser log</title>
    
    
 </head>
 <body>
    <script>
          
          var xvector = [31,-41,59,26,-53,58,97,-93,-23,84];
          
          //reverse order
          function findLargestNum(xvector){
              var xvector = xvector;
              var reverseCount = xvector.length - 1;    
              var halfOfVector = 0;
              var positionOne = 0;
              var positionTwo = 0;
              var totalMax = 0;
              var someargs = [];
              var largeOne = 0;
              var largeTwo = 0;
              var i = [];
              var j = [];
              
              halfOfVector = xvector.length / 2;
              
              //Thought: iteration will need to be Math.pow(n,2);
              
              //console.log("half of :"+halfOfVector+"");
              
              //store the position of largest numbers in list array
              
              for(let x = 0;  x <= halfOfVector - 1; x++){
              
                 i[x] = xvector[x];
              
                 //console.log("only five "+i+"");
                 //console.log("complete vertex "+ xvector +"");
                 
              }
              
              for(let y = 0; y <= halfOfVector - 1; y++){
    
                 j[y] = xvector[reverseCount];
              
                 //console.log("only last five "+j[y]+"");
                 //console.log("complete vertex "+ xvector +"");
                 
                 //console.log(reverseCount);
                 
                 reverseCount--;
                 
              }

              largeOne = Math.max.apply(null, i);
              largeTwo = Math.max.apply(null, j);
               
              for(let pos = 0; pos <= xvector.length - 1; pos++ ){
              
                   //console.log("position "+pos+" number "+xvector[pos]+" large "+largeOne+" largetwo "+largeTwo+"");
                   
                   if(xvector[pos] == largeOne){
                            positionOne = pos;
                   }
                    
                   if(xvector[pos] == largeTwo){
                            positionTwo = pos;
                   }
    
              }
              
              for(let pos = positionOne; pos <= positionTwo; pos++ ){
                    
                    console.log("adding number "+xvector[pos]+" to totalMax var");
                    totalMax += xvector[pos];
            
              }
              
              return totalMax;
              
              console.log("totalMax from position "+positionOne+" "+xvector[positionOne]+" to "+xvector[positionTwo]+" "+ positionTwo +" seems to be "+totalMax+"");
              console.log("large x :"+largeOne+"");
              console.log("large y :"+largeTwo+"");
              console.log("complete vertex "+ xvector +"");
              
          }
            
    </script>
 </body>
</html>

Resources

"Programming Pearls" (http://www.informit.com/store/programming-pearls-9780201657883)

Programing Pearls

"JavaScript: The Definitive Guide" (http://shop.oreilly.com/product/9780596805531.do)

JavaScript: The Definitive Guide
Z Data Tech https://www.zdatatech.com/logo/zdatatech-logo.png Last modified: October 21, 2017

Recent Posts