<!-- JavaScript:Basics code snippet 6. --> <!-- Collecting user input and performing numeric calculations. --> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en"> <!-- Code source: http://www.justfigures.co.uk/ --> <!-- A resource for web developers using XHTML, CSS, JavaScript, PHP --> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>View code snippet page</title> </head> <body> <script language="JavaScript" type="text/javascript"> <!-- // declare multiple variables in a single line var number1st, number2nd, answer; // initialise variables by assigning values number1st = window.prompt('Enter first number', ''); // convert input to numeric data type number1st = parseFloat(number1st); number2nd = window.prompt('Enter second number', ''); number2nd = parseFloat(number2nd); // note use of brackets for correct calculation order answer = (number1st + number2nd)/2; // use of quotes with strings and variables // note use of + for string concatenation document.write('The average of '+number1st+' and '+number2nd+' is '+answer); // --> </script> </body> </html>