0% found this document useful (0 votes)
25 views

Lab 3

The document provides instructions for Lab 3, which involves writing JavaScript code to take user input and return output. Students are asked to write functions for: 1) converting weight from pounds to kilograms, 2) calculating tip amount from meal cost and percentage, 3) generating a username from name and birth year input, and 4) converting temperatures between Fahrenheit and Celsius. The lab aims to practice using functions and taking/returning values to/from the user. Students must complete and test the code before submitting their work.

Uploaded by

ASS
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
25 views

Lab 3

The document provides instructions for Lab 3, which involves writing JavaScript code to take user input and return output. Students are asked to write functions for: 1) converting weight from pounds to kilograms, 2) calculating tip amount from meal cost and percentage, 3) generating a username from name and birth year input, and 4) converting temperatures between Fahrenheit and Celsius. The lab aims to practice using functions and taking/returning values to/from the user. Students must complete and test the code before submitting their work.

Uploaded by

ASS
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

CPSC 1045: Lab 3

Overview

The purpose of today’s lab is to create a JavaScript to practice taking input from the user and returning output
to the user. This lab will also allow you to practice using functions to accomplish small programming tasks.

Instructions:

Complete the following exercises individually. Please follow the instructions carefully and complete all of the
steps.

Step 1: Extracting Files

a) Download the attached zip file, Lab3.zip.


b) Unzip the folder and rename it as Lab3_FirstLastName on desktop or any other location of your choice.
Open up Visual Studio Code (You can also use any other text editor, but the steps are relevant to VS
code).
c) Add the above folder to the VS Code workspace.
d) Open the files lab3.html and lab3.js.

Step 2: Writing the code

In lab3.js, write the JavaScript code to complete the following exercises.


a) Write a function called convertWeight() that prompts the user to enter their weight in pounds and then
displays that weight in kilograms in div1 on webpage. One pound is 0.454 kilograms. You must round
off all the answers to two decimal units.
b) Write a function called calculateTip() that prompts the user to enter the cost of the meal as well as
how much they would like to tip (as a percentage). The function should then display a summary in
div2 on the webpage. The summary should contain the cost and percentage entered by the user, as
well as the amount of the tip, and the grand total of the meal. You must round off all the answers to
two decimal units.
c) Write a function called makeUsername() that prompts the user to enter three separate pieces of
information to store in variables: the first name, the last name, and the 4 digit birth year.
The function should then print a username to the webpage. The user name should contain the first
letter of the first name, followed by the first 4 letters of the last name, followed by the last two digits
of the birth year. The first two letters of the user name should be capitalized, and the remaining letters
should be lower cased.
d) Write two functions for Temperature Conversion, farToCel() and celToFar(). The function should then
display a summary in div4 on the webpage. You must round off all the answers to two decimal units.

• There are two buttons. The first button will convert from Farenheit to Celsius and the second
Celsius to Farenheit.
• If the user clicks on the F to C button, prompt them for a temperature in degrees Farenheit.
Then, in the output div, display a message such as
• 50 degrees F is equal to 10 degrees C.
• If the user clicks on the C to F button, the user should be asked for a temperature in Celsius
and display the conversion to Farenheit.
• Use the following formulas to do this.
• C = (F – 32) * 5 / 9
• F = (C * 9 / 5) + 32

Test that your JavaScript correctly executes for all of the problems by opening lab3.html in the browser
and testing all of the buttons.

2. When complete, your page should look similar to this screenshot.

Submission

Submit your zip file to the Lab3 Submission Folder on D2L.

• When you have completed all the exercises, call over the instructor or lab assistant. Be prepared to explain
or elaborate on any of the questions you answered in this exercise. You must complete as many exercises
as possible before leaving the lab today. No late exercises will be accepted. If unfinished during the lab, then
submit to D2L by end of the same day.

Grading
Marks will be assigned as follows:
o 3 marks for correctly implementing exercise A
o 3 marks for correctly implementing exercise B
o 3 marks for correctly implementing exercise C
o 4 marks for correctly implementing exercise D
o 2 marks for proper indentation and formatting

You might also like