Question

In: Computer Science

Create a JavaScript program of objects that asks the user how old they are in years....

Create a JavaScript program of objects that asks the user how old they are in years. Then ask the user if they would like to know how old they are in months, days, hours, or seconds. Use if/else conditional statements to display their approximate age in the selected timeframe. Perform all calculations using an AgeConverter class that accepts the age in years during initialization and has separate properties and methods to calculate and return the age in months, days, hours, and seconds. Include data validation in the class and error handling in the main program by using Node Js as IDE.

Solutions

Expert Solution

JS CODE:

class AgeConverter

{

constructor(age)

{

this.age=age;

}

ageMonths()

{

return this.age*12;

}

ageDays()

{

return Math.ceil(this.ageMonths()*30.44);

}

ageHours()

{

return this.ageDays()*24;

}

ageSeconds()

{

return this.ageHours()*60*60;

}

}

var age=prompt("Enter your age in years: ");

obj=new AgeConverter(age);

console.log("1: Convert age in months");

console.log("2: Convert age in days");

console.log("3: Convert age in hours");

console.log("4: Convert age in seconds");

var choice=prompt("Enter your choice: ");

if(choice==1)

console.log("Age in months: "+obj.ageMonths());

else if(choice==2)

console.log("Age in days: "+obj.ageDays());

else if(choice==3)

console.log("Age in hours: "+obj.ageHours());

else if(choice==4)

console.log("Age in seconds: "+obj.ageSeconds());

else

console.log("Invalid choice");

Sample outputs:


Related Solutions

Create a JavaScript program that asks for the input of at least five words (a total...
Create a JavaScript program that asks for the input of at least five words (a total of 25+ characters) string from a user and performs the following functions to it: STRING METHODS Check to see if the string input meets the 25+ character limit. If it does not, send a message and ask for another string. Output the original string as it was entered onto the web page document. Output the original string in all lower case letters. Do not...
Write a javascript program that asks the user to enter up to 10 golf scores, which...
Write a javascript program that asks the user to enter up to 10 golf scores, which are to be stored in an array. You should provide a means for the user to terminate input prior to entering 10 scores. The program should display all the scores on one line and report the average score. Handle input, display, and the average calculation with three separate array processing functions.
Create a program that asks the user for the names of two car dealerships and the...
Create a program that asks the user for the names of two car dealerships and the # of cars sold in each one. Then output that data in two columns as shown below. The "Store location" column has a width of 25, while the "Cars sold" column has a width of 9. Also, notice the alignment of the second column. The program should end with the "Press Enter to end this program" prompt. OUTPUT Enter the location for the first...
Q1 Create a program that asks the user to input their budget. Then, it will ask...
Q1 Create a program that asks the user to input their budget. Then, it will ask the user to input each of their purchase until their entire budget is used up. What kind of loop do you think is the most appropriate? You don't have to create the entire program. Part of it has already been created for you (see code below). Note: Assume that the user always uses up their budget to the last cent and they don't over...
Create a Java program that asks a user to enter two file names. The program will...
Create a Java program that asks a user to enter two file names. The program will read in two files and do a matrix multiplication. Check to make sure the files exist. first input is the name of the first file and it has 2 (length) 4 5 6 7 Second input is the name of the second file and it has 2 (length) 6 7 8 9 try catch method
Using C++ Create a program that asks the user to input a string value and then...
Using C++ Create a program that asks the user to input a string value and then outputs the string in the Pig Latin form. - If the string begins with a vowel, add the string "-way" at the end of the string. For “eye”, it will be “eye-way”. - If the string does not begin with a vowel, first add "-" at the end of the string. Then rotate the string one character at a time; that is, move the...
IN JAVA Create a program that asks the user to input the day, high and low...
IN JAVA Create a program that asks the user to input the day, high and low temperature. Display the day, high, and low. Use a While Loop to enter all seven days of the week and high temperatures. Inside the While Loop, the program will count, total, and average the highs. The following will be displayed:                         Day: (variable)                         High: (variable)                         Count High: (variable)                         Total High: (variable)                         Average High: (variable) After the seven days and highs...
IN JAVA Create a program that asks the user to input the day, high and low...
IN JAVA Create a program that asks the user to input the day, high and low temperature. Display the day, high, and low. Use a While Loop to enter all seven days of the week and high temperatures. Inside the While Loop, the program will count, total, and average the highs. The following will be displayed:                         Day: (variable)                         High: (variable)                         Count High: (variable)                         Total High: (variable)                         Average High: (variable) After the seven days and highs...
Create a program that asks the user to input three numbers and computes their sum. This...
Create a program that asks the user to input three numbers and computes their sum. This sounds simple, but there's a constraint. You should only use two variables and use combined statements. You can use the output below as a guide. Please enter the first number: 4 Please enter the second number: 2 Please enter the third number: 9 The sum of the three numbers is: 15.
Create a SPIM program that asks the user for a character string and a number. Repeatedly...
Create a SPIM program that asks the user for a character string and a number. Repeatedly display the string on the console for the number of times specified by the user. Format your assembly program to make it easier to read by including appropriate comments and formatting using appropriate white space. When ending your program call system call code 10. Example Output: Please enter a string: I will not repeat myself Please enter the integer number of times to repeat:...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT