repl.it
@HristoGueorguie/

FizzBuzz Solver without conditional logic

HTML, CSS, JS

A FizzBuzz solver that uses an array and array index calculation to map the correct answer.

fork
loading
Files
  • index.html
index.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
<!DOCTYPE html>
<html>
<head>
	<title>FizzBuzz MapSolver!</title>

</head>
<body onload="">

<canvas id="runTimesChartCanvas" width="200" height="200"></canvas>

<div id="dSSDisplay">10</div>
<div>
	<button type="button" onclick="main()">Compute!</button>
	<input type="range" min="10" max="10000000" value="10" class="slider" id="dSS" onchange="sliderUpdate()">
</div>

<!-- performance.now() -->
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/Chart.min.js" integrity="sha256-Uv9BNBucvCPipKQ2NS9wYpJmi8DTOEfTA/nH2aoJALw=" crossorigin="anonymous"></script>

<script type="text/javascript">

var runTimesChart=null;

function sliderUpdate(){
	let val = document.getElementById('dSS').value;
	document.getElementById('dSSDisplay').innerHTML = val;	
	console.log(val);
}

function buildChart(rawData, dSS){
	let timeData = {
	    type: 'bar',
	    data: {
	        labels: ['Standard', 'Ternary', 'MapSolver', 'MapSolver "Optimized"'],
	        xAxisID: 'Time to process '+ dSS +' values',
	        yAxesID: 'Compute function',
	        datasets: [{
	            label: 'FizzBuzz solvers run times',
	            data: rawData,
	            backgroundColor: [
	                'rgba(255, 99, 132, 0.2)',
	                'rgba(54, 162, 235, 0.2)',
	                'rgba(255, 206, 86, 0.2)',
	                'rgba(75, 192, 192, 0.2)'
	            ],
	            borderColor: [
	                'rgba(255, 99, 132, 1)',
	                'rgba(54, 162, 235, 1)',
	                'rgba(255, 206, 86, 1)',
	                'rgba(75, 192, 192, 1)'
	            ],
	            borderWidth: 1
	        }]
	    },
	    options: {
	        scales: {
	            yAxes: [{
	                ticks: {
	                    beginAtZero: true
	                }
	            }]
	        }
	    }
	}

	return timeData;
}

function drawChart(timeData){
  if (runTimesChart) runTimesChart.destroy();
	var ctx = document.getElementById('runTimesChartCanvas').getContext('2d');
	runTimesChart = new Chart(ctx, timeData);
}

	function FBSolve_Standard(number){
		if ( (number%5 == 0) && (number%3 == 0) ) return 'FizzBuzz';
		if ( number%3 == 0) return 'Fizz';
		if ( number%5 == 0) return 'Buzz';
		return number.toString();
	}

	function FBSolve_Ternary(number){
		answer=''; 
		if(!(number%3)) answer = 'Fizz'; 
		if(!(number%5)) answer+='Buzz';    
		return !answer? number: answer.toString();
	}

	function FBSolve_Map(number, answerkey){
	
		answer = answerkey[0+!((number%3)/(number%3))] + 
				 answerkey[(!((number%5)/(number%5)) + 0)*2] + 
				 answerkey[!(!((number%5)/(number%5)) || !((number%3)/(number%3))) * 3];
	
		return answer;
	}

	function FBSolve_Map_Optimized(number, answerkey){
		
		let mod3 = !((number%3)/(number%3));
		let mod5 = !((number%5)/(number%5));

		answer = answerkey[0+mod3] + 
				 answerkey[(mod5 + 0)*2] + 
				 answerkey[!(mod5 || mod3) * 3];
	
		return answer;
	}


	function main(){ 
	
		runTimes = [];
		inputs = [];
		dataSetSize = document.getElementById('dSS').value;
		for ( i = 0; i < dataSetSize; i++ ) inputs[i] = Math.floor(Math.random() * 10000);

		console.log('Running Standard Solver...');
		console.time('Standard');
		a = performance.now();
		for ( let input of inputs)
			FBSolve_Standard(input);
		b = performance.now();
		console.timeEnd('Standard');
		runTimes.push(b-a);
				//console.log(FBSolve_Standard(input));

		console.log('Running Ternary Solver...');
		console.time('Ternary');
		a = performance.now();
		for ( let input of inputs)
			FBSolve_Ternary(input);
		b = performance.now();
		console.timeEnd('Ternary');
		runTimes.push(b-a);
			//console.log(FBSolve_Ternary(input));

		console.log('Running Map Solver...');
		console.time('Map');
		a = performance.now();
		answerkey = ['', 'Fizz', 'Buzz', 0];
		for ( let input of inputs){
			answerkey[3] = input;
			FBSolve_Map(input, answerkey);
		}
		b = performance.now();
		console.timeEnd('Map');
		runTimes.push(b-a);
			//console.log(FBSolve_Map(input));

		console.log('Running Optimized Map Solver...');
		console.time('MapOp');
		a = performance.now();
		answerkey = ['', 'Fizz', 'Buzz', 0];
		for ( let input of inputs){
			answerkey[3] = input;
			FBSolve_Map_Optimized(input, answerkey);
		}
		b = performance.now();
		console.timeEnd('MapOp');
		runTimes.push(b-a);
			//console.log(FBSolve_Map(input));

		drawChart(buildChart(runTimes, dataSetSize));
	}

</script>
</body>
</html>
result
console
?