Saturday 24 October 2009

JavaScript

Here are some of the JavaScript programs we've been working on.

Adding VAT to an Amount
<script language='JavaScript'>
a=parseFloat(prompt('Enter cost',0)); // ask user for amount to add vat to
cost=a; // set variable cost to the amount entered by user for a
vat=cost*17.5/100; // set variable vat to the amount of cost multiplied by 17.5 and divided by 100
document.write('£',cost,'+VAT at 17.5% =','£',cost+vat); // write sum of cost and vat to screen
</script>

Adding Numbers
<script language='JavaScript>
a=parseFloat(prompt('Number to start from?',0)); // ask user for number to start adding from
b=parseFloat(prompt('Number to stop at?',0)); // ask user for number to stop adding to
count=a; // set variable count to amount set by user in a
sum=0; // set variable sum to zero
while (count<b) { // while count is less than the amount the user entered for b do the following
count=count+1; // add one to the amount of count
sum=sum+count; // add the amount of count to the amount of sum
}
document.write(sum); // write amount of sum to screen
</script>

Creating a Slash made of d's
<script language='JavaScript'>
a=parseFloat(prompt('How many rows?',0)); // ask for user to input number of rows
row=0; // set variable row to zero
while (row<a) { // while row is less than the number inputted by the user for a do the following
space=0; // set variable space to zero
while (space<row) { // while space is less than zero do the following
document.write(" "); // write a space to the screen that the browser will not ignore
space=space+1; // add one to the amount of space
}
document.write("d<br>"); // write the letter d to the scren
space=0; // set variable space to zero
row=row+1; // add one to the amount of rows
}
</script>
  • Stumble This
  • Fav This With Technorati
  • Add To Del.icio.us
  • Digg This
  • Add To Facebook
  • Add To Yahoo

No comments:

 
Copyright 2010 Meow