Question

In: Computer Science

Must use prompts to get input from user and alerts for output. Do not use jQuery...

Must use prompts to get input from user and alerts for output. Do not use jQuery or document.getElementById to solve these problems.

User input validation is not required. Assume the user will enter correct values and write your program. Program should run once and exit.

1. Filenames: tip.html & tip.js
Write a Tip program where the user enters a restaurant bill total. The program should then display two amounts: a 15 percent tip and a 20 percent tip. The output should look similar to this:

Total bill: 44.9

15% tip = 6.735 and 20% tip = 8.98

2. Filenames: Tax.html & Tax.js
Write a check out program that calculates the total cost (total price of items + 6% sales tax) of buying two items. Use floats for this problem and format them to 2 decimal places before printing them. The output should look similar to this:

Cost of Item1: 40.0
Cost of Item2: 14.55

Total price of two items: 54.55

6% Sales tax = 3.27

Total cost = 57.82

Always format numbers after all calculations have been completed and your program is ready to display the result.

Formatting to a specific decimal place using the toFixed method:

var num = 2347.67890;

num = num.toFixed(2); -> This converts the num into a string

alert(num); -> 2347.67

alert(num.toFixed(2)) should be good for display purposes. There is no need to convert and update the variable num.

Solutions

Expert Solution

tip.html:

<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>Tip</title>
<script type="text/javascript" src="tip.js"></script>
<script>
var bill=window.prompt("Enter total bill");
var tip1=calculate15Tip(bill);
var tip2=calculate20Tip(bill);
var text="Total Bill:"+bill+"\n15% tip ="+tip1+" and 20% tip ="+tip2;
alert(text);

</script>
</head>
<body>

</body>
</html>

tip.js:

function calculate15Tip(bill){
   var num=0.15*bill;
   return num.toFixed(2);
}
function calculate20Tip(bill){
   var num=0.20*bill;
   return num.toFixed(2);
}

Expected output:

tax.html:

<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>Tax</title>
<script type="text/javascript" src="tax.js"></script>
<script>
var cost1=window.prompt("Enter cost of item 1:");
var cost2=window.prompt("Enter cost of item 2:");
var tax=calculateTax(cost1,cost2);
var totalprice=calculateTotalwoTax(cost1,cost2);
var finalprice=calculateTotalCost(cost1,cost2);
var text="Cost of item1: "+cost1+"\nCost of item2: "+cost2+"\nTotal price of two items: "+totalprice+
       "\n6% Sales tax = "+tax+"\nTotal cost = "+finalprice;
alert(text);
</script>
</head>

<body>

</body>
</html>

tax.js:

function calculateTax(cost1,cost2){
   var tax=(parseFloat(cost1)+parseFloat(cost2))*0.06;
   return tax.toFixed(2);
}
function calculateTotalwoTax(cost1,cost2){
   var cost=parseFloat(cost1)+parseFloat(cost2);
   return cost.toFixed(2);
}
function calculateTotalCost(cost1,cost2){
   return (parseFloat(calculateTotalwoTax(cost1, cost2))+parseFloat(calculateTax(cost1, cost2))).toFixed(2);
}

Expected output:


Related Solutions

Write a program that prompts the user to input their first name from the keyboard and...
Write a program that prompts the user to input their first name from the keyboard and stores them in the variable "firstName". It does the same for last name and stores it in the variable "lastName". It then uses strcat to merge the two names, separates them by a space and stores the full name into a string variable called "fullName". In the end, the program must print out the string stored within fullName. ANSWER IN C LANGUAGE ! You...
Prompt the user for their name, get and store the user input. Prompt the user for...
Prompt the user for their name, get and store the user input. Prompt the user for their age, get and store the user input. We will assume that the user will enter a positive integer and will do no error checking for valid input. Determine and store a movie ticket price based on the user's age. If their age is 12 or under, the ticket price is $5. If their age is between 13 and 64, inclusive, the ticket price...
Write a program that prompts the user to input a string. The program then uses the...
Write a program that prompts the user to input a string. The program then uses the function substr to remove all the vowels from the string. For example, if str=”There”, then after removing all the vowels, str=”Thr”. After removing all the vowels, output the string. Your program must contain a function to remove all the vowels and a function to determine whether a character is a vowel. You must insert the following comments at the beginning of your program and...
1.Prompts the user for a positive integer >= 0 2.Validates the user input to ensure it...
1.Prompts the user for a positive integer >= 0 2.Validates the user input to ensure it is a positive integer >= 0 3.Allocate (dynamically) an array big enough for the data. 4.Load the array with random numbers ranging in value from1 to 100 5.Display the elements of the array (unsorted) 6.Display the elements of the array (sorted) 7. Display the average 8.Display the median 9.Display the mode, if none, display appropriate message #include <iostream> #include <stdlib.h> /* srand, rand */...
Define a java problem with user input, user output, Do While Statement and some mathematical computation....
Define a java problem with user input, user output, Do While Statement and some mathematical computation. Write the pseudocode, code and display output.
In Coral Code Please!!!! The assignment is to get an integer from input, and output that...
In Coral Code Please!!!! The assignment is to get an integer from input, and output that integer squared, ending with newline. (Note: This assignment is configured to have students programming directly in the zyBook. Instructors may instead require students to upload a file). Below is a program that's been nearly completed for you. Click "Run program". The output is wrong. Sometimes a program lacking input will produce wrong output (as in this case), or no output. Remember to always pre-enter...
⦁   Write a Bash script that prompts for user input and reads a string of text...
⦁   Write a Bash script that prompts for user input and reads a string of text from the user. If the user enters a no null (no empty) string, the script should prompt the user to re-enter again the string; otherwise should display the string entered “. ⦁   Given an array of the following four integers (3, 5, 13, 14); write a Bash script to display the second and all elements in the array.    ⦁   Write a short Bas...
Write a Java program that prompts the user to input a string and prints whether it...
Write a Java program that prompts the user to input a string and prints whether it is a palindrome. A palindrome is a string which reads the same backward as forward, such as Madam (disregarding punctuation and the distinction between uppercase and lowercase letters). The program must use the stack data structure. The program must include the following classes: The StackX class (or you can use the Java Stack class). The Palindrome class which must contain a method named palindrome()...
Write a program in c++ that prompts the user to input a coin collection of number...
Write a program in c++ that prompts the user to input a coin collection of number of quarters, dimes, nickels and pennies. The program should then convert the coin collection into currency value as dollars. The coin values should all be whole numbers and the resulting currency value should be displayed with two decimals. An example of user interaction is as follows: Coin Convertor Enter number of quarters: 3 Enter number of dimes: 1 Enter number of nickels: 4 Enter...
(JAVA) Create a program that prompts the user for an age input. The program defines the...
(JAVA) Create a program that prompts the user for an age input. The program defines the age group of the user. Follow the table below to construct the output for your program. Age Age Group 0 Baby 1-3 Toddler 4-11 Child 12-17 Teenager 18-21 Young Adult 22-64 Adult 65+ Senior Negative Number Invalid Input Sample Input Enter an age: 18 Sample Output: You are a young adult. Sample Input Enter an age: 29 Sample Output: You are an adult. Sample...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT