Working with Global Functions - Exercise

Welcome to our free JavaScript tutorial. This tutorial is based on Webucator's Introduction to JavaScript Training course.

Working with Global Functions - Exercise

Working with Global Functions

Duration: 10 to 15 minutes.

In this exercise, you will practice working with JavaScript's global functions.

1. Open JavaScriptFunctions/Exercises/BuiltinFunctions.html for editing.
2. Modify the file so that it outputs the sum of the two numbers entered by the user.

Code Sample:

JavaScriptFunctions/Exercises/BuiltinFunctions.html
```<!DOCTYPE HTML>
<html>
<meta charset="UTF-8">
<title>JavaScript Built-in Functions</title>
<script>
userNum1 = window.prompt("Choose a number.", "");
userNum2 = window.prompt("Choose another number.", "");
</script>
<body>
<p>
<script>
document.write(userNum1 + " + " + userNum2 + " = ");
</script>
</p>
</body>
</html>
```

Challenge

Create a new HTML file that prompts the user for his name, the age at which he first worked on a computer, and his current age. After gathering this information, pop up an alert that tells the user how many years he's been working on a computer. The images below show the steps:

Notice that the program is able to deal with numbers followed by strings (e.g., "12 years old").

Solution:

JavaScriptFunctions/Solutions/BuiltinFunctions.html
```<!DOCTYPE HTML>
<html>
<meta charset="UTF-8">
<title>JavaScript Built-in Functions</title>
<script>
userNum1 = window.prompt("Choose a number.", "");
userNum2 = window.prompt("Choose another number.", "");
</script>
<body>
<p>
<script>
document.write(userNum1 + " + " + userNum2 + " = ");
</script>
</p>
</body>
</html>
```

Challenge Solution:

JavaScriptFunctions/Solutions/BuiltInFunctions-challenge.html
```<!DOCTYPE HTML>
<html>
<meta charset="UTF-8">
<title>JavaScript Built-in Functions</title>
<script>