<script> // FizzBuzz /* Write a program that uses console.log to print all the numbers from 1 to 100, with two exceptions. For numbers divisible by 3, print "Fizz" instead of the number, and for numbers divisible by 5 (and not 3), print "Buzz" instead. When you have that working, modify your program to print "FizzBuzz", for numbers that are divisible by both 3 and 5 (and still print "Fizz" or "Buzz" for numbers divisible by only one of those). */ var arr = [...Array(100).keys()].map((d) => d+1); var divThree = arr.map((d) => {if(d%3==0)return 1; else return 0; }); var divFive = arr.map((d) => {if(d%5==0)return 1; else return 0; }); var outputFizz = arr.map((d,i) => { if(divThree[i]) return "Fizz: " + d; else return d; }); var outputBuzz = outputFizz.map((d,i) => { if(divFive[i] && !divThree[i]) return "Buzz: " + d; else return d; }); var outputFizzBuzz = arr.map((d,i) => { if(divThree[i] && divFive[i]) return "FizzBuzz: " + d; else return outputBuzz[i]; }); //console.log(outputFizzBuzz); //ok, this works //////////////////////////////////////////// //now how could we make this chainable? //maybe make it a method of the array prototype Array.prototype.divThree = function(){ this.map((d,i) => {if(d%3==0) this[i]=1; else this[i]=0; }); return this; } Array.prototype.divFive = function(){ this.map((d,i) => {if(d%5==0) this[i]=1; else this[i]=0; }); return this; } //console.log(arr.divFive()) //this works, as far as defining new functions that do the filtering //but, we're always defining a new function, and also they change the //array into the bit mask.... ////////////////////////////////////////////////// //what if we want to define our filtering functions separately function divThreeFunc(d){ if(d%3==0)return 1; else return 0; } function divFiveFunc(d){ if(d%5==0)return 1; else return 0; } //console.log(arr.map((d) => divThreeFunc(d))) //console.log(arr.map((d) => divFiveFunc(d))) //creating the bitmasks in this way works fine.... //what about built-ins, like using filter.... var filteredArray = arr.filter((d) => {if(d%3==0) return d;}); //console.log(filteredArray) //this works, but it changes the size of the array, which might be fine //on output, but we'd rather use the bitmasks when we're doing in-progress, //step by step computation //so we need something like the mask input to each modifier node (like gamma, multiply) //arr.multiply(2, mask=[0,1,1,0,...,0,1]) //we want a filter that's more like, applyMask //arr.applyMask(mask=alpha); var applyDivThree = arr.map((d,i) => { return d * divThree[i]; }); //console.log(applyDivThree); //how does the actual apply function work... //console.log(Math.max.apply(null, arr)); Array.prototype.applyMask = function(mask){ //if no mask supplied, use an array of all 1's //mask = mask || [...Array(this.length).keys()].map((d) => { return 1;} ) //element-wise multiply by mask //this.map((d,i) => { return d * mask[i]; }); return this.map((d,i) => d * mask[i]); } //console.log(divThree) console.log(arr.applyMask(divThree)); console.log(arr.applyMask(divFive)); </script>