(SOLVED) MyCCTC usernames, they consist of the student's first name initial, and full last name
Discipline: Mathematics
Type of Paper: Question-Answer
Academic Level: Undergrad. (yrs 3-4)
Paper Format: APA
Question
With MyCCTC usernames, they consist of the student's first name initial, and full last name. Using JavaScript, create a username for the following students: Tuan Mecann • Faustino Campbell • Ariel Cook • Buck Harris • Preston Trujillo • Lawanda Gardner • Ilene Hebert • Elnora Webster May Reeves Amie Woodward Use the document.write(), prompt() functions, and JavaScript string operators to display the usernames on the screen. 2. Using the following variables, to create expressions using the addition, subtraction, division, modulus, decrement, exponentiation arithmetic operators. var x = 5 var y = 10 var z=50 3. Using JavaScript's ternary operator, create that displays the cost of a movie ticket for a customer. The user should be prompted for the age of the customer (use the prompt() function). The output should be a message telling the user what the customer's ticket will cost, based on the following criteria: a. Under age 5 entry is free b. Between ages 5 and 12 (inclusive) a child's ticket costs $5.00 c. Older than 12 an adult ticket costs $9.00
Expert Answer
1. We have been provided with the task to take full names and generate user names in the format used in MyCCTC usernames, where only the first initial letter of the first name and the full last name is used.
Suppose for instance your name is
let name = "John Doe"
For the first initial letter of the first name we use, name[0] which gives us the first element of the name string. [ In this case J ]
Now, name.indexOf(" ") will identify the index of the blank in the name string, that is the point after which the last name starts.
So. name.slice(name.indexOf(" ") + 1) will give us all the characters of the last name, that is in this case "Doe". [+1 because we do not want to include the blank].
So final user name should be
name[0] + name.slice(name.indexOf(" ") + 1)
In our example case of "John Doe" we get generated username as "JDoe"
Below is an example code, demonstrating the same :
function create_username(fullname) {
let username = fullname[0] + fullname.slice(fullname.indexOf(" ") + 1);
return username;
}
let input_name = prompt("Hello there, what is your name?");
let created_username = create_username(input_name);
document.write(
"The generated username for " + input_name + " is : " + created_username
);
let tuan_mccann = create_username("Tuan Mccann");
let faustino_campbell = create_username("Faustino Campbell");
let ariel_cook = create_username("Ariel Cook");
let buck_harris = create_username("Buck Harris");
let preston_trujillo = create_username("Preston Trujillo");
let lawanda_gardner = create_username("Lawanda Gardner");
let ilene_hebert = create_username("Ilene Hebert");
let elnora_webster = create_username("Elnora Webster");
let may_reeves = create_username("May Reeves");
let amie_woodward = create_username("Amie Woodward");
document.write("
");
document.write("
");
document.write("
More examples of our username generator are :");
document.write("
");
document.write(
"
The generated username for Tuan Mccann is : " + tuan_mccann
);
document.write(
"
The generated username for Faustino Campbell is : " + faustino_campbell
);
document.write("
The generated username for Ariel Cook is : " + ariel_cook);
document.write(
"
The generated username for Buck Harris is : " + buck_harris
);
document.write(
"
The generated username for Preston Trujillo is : " + preston_trujillo
);
document.write(
"
The generated username for Lawanda Gardner is : " + lawanda_gardner
);
document.write(
"
The generated username for Ilene Hebert is : " + ilene_hebert
);
document.write(
"
The generated username for Elnora Webster is : " + elnora_webster
);
document.write("
The generated username for May Reeves is : " + may_reeves);
document.write(
"
The generated username for Amie Woodward is : " + amie_woodward
);
----------------------------------------
2. The predefined values of the three variables x , y and z are 5 , 10 and 50 respectively.
Required Arithmetic Operations are as follows:
Addition : Finding the sum of two variables. Example : 5 + 10 = 15
Subtraction : Finding the difference of two variables. Example : 5 - 10 = -5
Division : Finding the answer of the division operation of two variables. Example : 5 / 10 = 0.5
Modulus : Finding the remaninder of the division operation of two variables. Example : 5 % 10 = 5
Decrement : Subtracting 1 from the exisiting value of the variable. Example : 5-- = 4
Exponentiation : The first variable raised to the power of the second one. 5**10 = 9765625
Example code implementing all the necessary operations is shown below:
var x = 5;
var y = 10; //declaring variables x , y and z with predefined initial values
var z = 50;
var addition = x + y + z; //adding x , y and z
alert("Addition arithmetic operation result 'x+y+z': " + addition);
var subtraction = x - y - z; //subtracting y and z from x
alert("Subtraction arithmetic operation result 'x-y-z': " + subtraction);
var divison_xy = x / y; //dividing x by y
var divison_yz = y / z; //dividing y by z
alert("Division arithmetic operation result 'x/y': " + divison_xy);
alert("Division arithmetic operation result 'y/z': " + divison_yz);
var modulus_xy = x % y; //finding the remainder of division operation of x and y
var modulus_yz = y % z; //finding the remainder of division operation of y and z
alert("Modulus arithmetic operation result 'x%y': " + modulus_xy);
alert("Modulus arithmetic operation result 'y%z': " + modulus_yz);
var decrement_x = x--; //subtracting 1 from the current value of x (post-decrement)
var decrement_y = --y; //subtracting 1 and changing the current value of x (pre-decrement)
alert("Decrement arithmetic operation result 'x--': " + decrement_x);
alert("Post Decrement arithmetic operation result '--y': " + decrement_y);
var exponentiation_xy = x ** y; //x raised to the power of y
var exponentiation_yz = y ** z; //y raised to the power of z
alert(
"Exponentiation arithmetic operation result 'x**y': " + exponentiation_xy
);
alert(
"Exponentiation arithmetic operation result 'y**z': " + exponentiation_yz
);
----------------------------------------
3. We use ternary operator to first check if the user's age is :
Less than 5
True : Ticket Price = free
False : We now check if age is Less than equal to 12 [as age is already greater than equal to 5, hence here we check for range (5 <= age <= 12) ].
True : This means user age is between 5 and 12. So Ticket Price = $5
False : This means user age is greater than 12. So Ticket Price = $9
Sample code for the above mentioned scenario is as follows :
function ticket_price(age) {
let price;
age < 5 ? (price = "free") : age <= 12 ? (price = "$5.00") : (price = "$9.00");
return price;
}
let user_age = parseInt(prompt("Please enter your age below :"));
let cost = ticket_price(user_age);
alert("Your final price for the ticket is " + cost);
----------------------------------------