Web Development and Design

Scripting

objective

When I first started learning script I was very interested but I wasn't sure if it was for me. When I started I found I really liked it, I really like the challenge it has given me.

This was a bit of simple script that was a challenge at first. I had to google miles to kilometer conversions so I could do the exercise.


var TotKm = prompt("Enter km to convert", 10);
var TotMiles = km_to_miles(TotKm);
alert("The total miles is: " + TotMiles + " miles.");
function km_to_miles (km) {
//var km = 10;
var miles = 0;
miles = km * 0.62;
return miles;
// alert("Your amount is: " + miles + " miles.");
}


For this bit of code I had to use parseInt to calculate the factorial of a number.

var userNum=parseInt(prompt("Please enter a number."));
var total=1;
/*calculating the factorial with the string input using parseInt*/
for (i=0; i<\userNum; i++){
total *= (userNum-i);
}
/*writing the 'answer'*/
document.write(total);


This script will calculate the factorial numbers greater than 0 and less than or equal to 20

var userNum = parseInt(prompt("Please enter a number with a value higher than \"0\" and lower than or equal to \"20\"."));
var total=1;
for (i=0; i<\userNum; i++){
total *=(userNum-i);
}
if (userNum <=0){
parseInt(alert("You chose the number \"" + userNum + "\". Please refresh the page and enter a number with a value higher than \"0\" and less than or equal to \"20\"."));
}
else if (userNum >21){
alert("You chose the number \"" + userNum + "\". Please refresh the page and enter a number with a value higher than \"0\" and less than or equal to \"20\".");
}
else {
document.write(total + " is the factorial of " + userNum);
}