Skip to content

Instantly share code, notes, and snippets.

@mojenmojen
Created September 26, 2016 01:06
Show Gist options
  • Save mojenmojen/2638d003e70b6fca1b4bfe364f3a0b72 to your computer and use it in GitHub Desktop.
Save mojenmojen/2638d003e70b6fca1b4bfe364f3a0b72 to your computer and use it in GitHub Desktop.
Define a method called check_divisors that takes three parameters, divisor_array (an Array) and low and high will be integers. The method should print all numbers from low to high; if any number being printed is divisible by any divisor number in divisor_array, print the number + the word "one_match". If the number being printed is divisible by …
/*
sample output:
if the function call were check_divisors( [2,3], 1, 7) the printed output would be:
1
2 one_match
3 one_match
4 one_match
5
6 all_match
7
*/
function check_divisors(divisor_array, low, high) {
// check to see how many items are in the divisor Array
arrayLength = divisor_array.length;
// make a loop from low to high for the numbers to print
for ( printNum = low; printNum <= high; printNum++ ) {
// reset the output String
checkMessage = "";
// reset the match counter
matchCounter = 0;
// loop through the elements in the divisor array
for ( j = 0; j < arrayLength; j++ ) {
// see if the current printNum is divisible by the current element in the divisor array
checkIfDivisible = printNum % divisor_array[j];
if ( checkIfDivisible === 0 ) {
// if it is divisible, then update the print message
checkMessage = "one_match";
// also increment the matchCounter
matchCounter++;
}
}
// if the number of matches equals the number of things in the array then update the message
if ( matchCounter == arrayLength ) {
checkMessage = "all_match";
}
console.log( printNum + " " + checkMessage);
}
return true;
}
check_divisors( [2,3], 1, 7);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment