Return to Snippet

Revision: 1783
at November 10, 2006 00:04 by whitetiger


Updated Code
function istogramma()
{
	/*
	 *
	 * Prende in input una serie di numeri, sia positivi che negativi
	 * e crea un istogramma. 
	 */
	 	
	var n = window.prompt('Numero di valori da inserire:', '');
	
	n = parseInt(n); // rende il valore se valido Int altrimenti NaN
	
	if(isNaN(n) == true)
	{
		document.write('<p align=\"center\"><b>Non hai selezionato nessun valore...</b></p>');
		document.write('<br>');
	}
	else
	{
		var num = new Array();
		var max = 0;
		
		for(var i=0; i<n; i++)
		{
			var tmp = window.prompt('Inserire Valore [' + (i+1) + '/' + n + ']', '');
			tmp = parseFloat(tmp);
			
			if(isNaN(tmp) == true)
			{
				n = i;
				break;
			}
			
			num[i] = tmp;
			
			if(max < Math.abs(num[i])) max = Math.abs(num[i]);
		}
		
		for(var i=0; i<n; i++) num[i] *= 100/max;
		
		document.write('<p align=\"center\"><b>Istogramma for JavaScript</b></p>');
		document.write('<br>');
		document.write('<table align=\"center\" border=\"0\" bgcolor=\"red\">');
		document.write('<tr><td>');
		document.write('<table align=\"center\" border=\"0\" cellspacing=\"0\" cellpadding=\"0\" width=\"200\" bgcolor=\"black\">');

		for(var j=0; j<n; j++)
		{
			document.write('<tr>');
			
			for(var i=0; i<200; i++)
			{
				var aux = num[j];
				
				if(num[j] == 0) document.write('<td width=\"1\" bgcolor=\"white\"> </td>');
				
				if(num[j] > 0 && i < 100) document.write('<td width=\"1\" bgcolor=\"white\"> </td>');
				else if(num[j] > 0 && i >= 100)
				{
					if((i-100) <= aux)
						document.write('<td width=\"1\" bgcolor=\"green\"> </td>');
					else
						document.write('<td width=\"1\" bgcolor=\"white\"> </td>');
				}
				
				if(num[j] < 0 && i < (100+aux)) document.write('<td width=\"1\" bgcolor=\"white\"> </td>');
				else if(num[j] < 0 && (i >= (100+aux) && i < 100)) document.write('<td width=\"1\" bgcolor=\"yellow\"> </td>');
				else if(num[j] < 0 && i >= 100) document.write('<td width=\"1\" bgcolor=\"white\"> </td>');
			}
			
			document.write('</tr>');
		}

		document.write('</table>');
		document.write('</td></tr>');
		document.write('</table>');
	}
}


<html>
	<head>
		<title>Disegnare un Istogramma...</title>
		<script src='istogramma.js' type='text/javascript'></script>
	</head>
	
	<body OnLoad="istogramma();">
	</body>
</html>

Revision: 1782
at November 9, 2006 23:55 by whitetiger


Initial Code
function istogramma()
{
	/*
	 *
	 * Prende in input una serie di numeri, sia positivi che negativi
	 * e crea un istogramma. 
	 */
	 	
	var n = window.prompt('Numero di valori da inserire:', '');
	
	n = parseInt(n); // rende il valore se valido Int altrimenti NaN
	
	if(isNaN(n) == true)
	{
		document.write('<p align=\"center\"><b>Non hai selezionato nessun valore...</b></p>');
		document.write('<br>');
	}
	else
	{
		var num = new Array();
		var max = 0;
		
		for(var i=0; i<n; i++)
		{
			var tmp = window.prompt('Inserire Valore [' + (i+1) + '/' + n + ']', '');
			tmp = parseFloat(tmp);
			
			if(isNaN(tmp) == true)
			{
				n = i;
				break;
			}
			
			num[i] = tmp;
			
			if(max < Math.abs(num[i])) max = Math.abs(num[i]);
		}
		
		for(var i=0; i<n; i++) num[i] *= 100/max;
		
		document.write('<p align=\"center\"><b>Istogramma for JavaScript</b></p>');
		document.write('<br>');
		document.write('<table align=\"center\" border=\"0\" bgcolor=\"red\">');
		document.write('<tr><td>');
		document.write('<table align=\"center\" border=\"0\" cellspacing=\"0\" cellpadding=\"0\" width=\"200\" bgcolor=\"black\">');

		for(var j=0; j<n; j++)
		{
			document.write('<tr>');
			
			for(var i=0; i<200; i++)
			{
				var aux = num[j];
				
				if(num[j] == 0) document.write('<td width=\"1\" bgcolor=\"white\">&nbsp;</td>');
				
				if(num[j] > 0 && i < 100) document.write('<td width=\"1\" bgcolor=\"white\">&nbsp;</td>');
				else if(num[j] > 0 && i >= 100)
				{
					if((i-100) <= aux)
						document.write('<td width=\"1\" bgcolor=\"green\">&nbsp;</td>');
					else
						document.write('<td width=\"1\" bgcolor=\"white\">&nbsp;</td>');
				}
				
				if(num[j] < 0 && i < (100+aux)) document.write('<td width=\"1\" bgcolor=\"white\">&nbsp;</td>');
				else if(num[j] < 0 && (i >= (100+aux) && i < 100)) document.write('<td width=\"1\" bgcolor=\"yellow\">&nbsp;</td>');
				else if(num[j] < 0 && i >= 100) document.write('<td width=\"1\" bgcolor=\"white\">&nbsp;</td>');
			}
			
			document.write('</tr>');
		}

		document.write('</table>');
		document.write('</td></tr>');
		document.write('</table>');
	}
}


<html>
	<head>
		<title>Disegnare un Istogramma...</title>
		<script src='istogramma.js' type='text/javascript'></script>
	</head>
	
	<body OnLoad="istogramma();">
	</body>
</html>

Initial URL

                                

Initial Description

                                

Initial Title
JavaScript - Simple Example Istogramma

Initial Tags
http, javascript, js, image, html, script, images, web

Initial Language
JavaScript