Author: Theodore Odeluga
The source files for this project can be downloaded from here
This is a Node command line project and Node.js can be downloaded from here
Instructions for running a command line program with Node.js can be found here
One kind of challenge you might consider for a problem-solving exercise is an alternative way to complete a particular task.
For example, conditional logic is normally executed with symbols such as the greater than operator > or the key word ‘if’ to test for specific conditions and values. The ‘case’ keyword may also be used in this regard.
What if however, you’re asked to perform a routine operation without conventional operators?
This can often be a good way to train yourself to think outside the box as it were. Considering alternative methods for solving problems can enhance your skills and broaden your range.
How about trying to perform a division operation without the ‘/’ division operator?
All of a sudden, something you commonly do without too much effort becomes a more challenging prospect.
To start breaking this problem down, one needs to think about what actually happens when a division operation is performed.
You can better imagine it with a physical analogy.
When dividing a pizza for example, the original object effectively becomes more than one object, with each piece now a unique item in its own separate location.
In this sense, you could see the separate physical instances which you have to count to account for one pizza as the number of times you have to count to process one object (such as a number being divided a certain number of times).
This is the roundabout way of doing a division which the divisor operator is normally shorthand for.
It’s this concept of “multiple counts” for one object (the dividend) that the function mydivide is based on.
In mydivide the two key variables, dividend and divisor are initialised with values of 12 and 2 respectively (feel free to play around with other values).
The function opens up with a while loop testing the iterator i for a maximum value equivalent to the dividend.
while(i < dividend){
i++;
dividendarray.push(i);
}
As this loop progresses, all the values counted from zero to the dividend are placed into the dividendarray object for temporary storage (this is the programmatic equivalent of that pizza we discussed in the above analogy). Here the pizza "pieces" are array elements and currently, they have not been separated; the array is still the singular representative object (soon to be divided).
The next loop is a nested structure with the outer loop traversing dividendarray with the j iterator.
for(j=0; j < dividendarray.length; j++){
k++;
if(k == divisor){
k = 0;
count++;
}
}
J’s purpose is to enable k (incrementing on the first line immediately after the outer loops opening) to ‘slice up’ the dividend array.
Each time a ‘piece’ is generated by k, k is reset to zero to repeat the process until ‘j’ reaches the end (the maximum value defined by the dividend).
if(k == divisor){
k = 0;
count++;
}
Each ‘piece’ generated by the actions of k is counted by the ‘count’ variable.
k = 0;
count++;
This enables the last line before the end of the function to output the results.
console.log(divisor + " divides " + dividend + " " + count + " times. ");
Dividend tells the user which number was divided. Divisor states which number did the dividing. Count shows the result of the dividing.
Here's the entire program for convenience.
let dividendarray = []; function mydivide(){ /*Break down the divisor while(i < dividend){ /*Iterate over the dividend array for(j=0; j < dividendarray.length; j++){
let dividend = 12;
let divisor = 2;
let count = 0;
let i = 0;
let j;
let k = 0;
into constituent numbers.*/
i++;
dividendarray.push(i);
}
and register each time the iterator
generates a value equivalent to
the divisor (keeping track of the
number of divisor value instances with
the 'count' flag (resetting 'k'
each time to prepare for the next
instance)).*/
k++;
if(k == divisor){
k = 0;
count++;
}
}
console.log(divisor + " divides " + dividend + " " + count + " times. ");
}
mydivide();
Conclusion
This example was a simple way to practice what you normally do with your code in a different way. Being able to take alternative approaches is a useful ability when faced with a test that complicates what might otherwise be a straightforward task. This is what makes programming sometimes awkward and difficult but also keeps it interesting. Try to think up your own ideas for alternative methods (i.e. – alternative ways to perform multiplication, addition or subtraction in code) and see what clever solutions you can come up with.