//Script to find numbers that are the power of 3 and 5 from 1 to 100 using % Modulus operator - when it finds a number that can be the power of 3 or 5 it outupts FizzBuzz... example hosted on my blog http://chadcollins.com/find-numbers-that-are-the-power-of-3-and-5-from-1-to-100/
// this was an interview question, and I want to know how to optimize or improve this with "pure javascript".
<div id=out_put></div>
//from a list of 1 to 100 numbers, find the 3's and 5's using modulus
function findPowerOf(numList) {
//setup the variables
var myModMatch1 = 3; //find numbers that have the power of 3
var myModMatch2 = 5; //find numbers that have the power of 5
var tempCheck1 = ""; //stores true or false based on myModMatch1
var tempCheck2 = ""; //stores true or false based on myModMatch2
var stringOut = ""; //concat string for output
var numListStart = 1; //starting number list index
var numListFinish = 100; //ending list index
var numberList = []; //create the list of numbers
for (var i = numListStart; i <= numListFinish; i++) {
numberList.push(i);
}
for (i = 0; i < numberList.length; i++) { //loop on each number and check the modulus params
console.log(numberList[i]);
if ((numberList[i] % myModMatch1) === 0) { //check first modulus param
console.log('houston, we have a match on a number modulus 3');
tempCheck1 = "Fizz";
}
if ((numberList[i] % myModMatch2) === 0) {
console.log('houston, we have a match on a number modulus 5');
tempCheck2 = "Buzz";
}
if (tempCheck1 === "" && tempCheck2 === "") { //no modulus matches
console.log("no modulus matches");
stringOut += numberList[i] + "\n";
}
stringOut += tempCheck1 + tempCheck2 + "\n";
tempCheck1 = ""; //clear both variables
tempCheck2 = "";
}
//dynamically make a div
var outDiv = document.createElement("div");
outDiv.innerHTML = stringOut; //output the final loop values all at once
document.getElementById('out_put').appendChild(outDiv); //update the view
}
findPowerOf(); // call our function