R
@Raza74
Ex:45
/*
• Cars: Write a function that stores information about a car in a Object. The function should always receive a manufacturer and a model name. It should then accept an arbitrary number of keyword arguments. Call the function with the required information and two other name-value pairs, such as a color or an optional feature. Print the Object that’s returned to make sure all the information was stored correctly.
*/
interface Car {
manufacturer: string;
model: string;
[key: string]:
Ex_44
/*
• Sandwiches: Write a function that accepts a array of items a person wants on a sandwich. The function should have one parameter that collects as many items as the function call provides, and it should print a summary of the sandwich that is being ordered. Call the function three times, using a different number of arguments each time.
*/
function makeSandwich(...ingredients: string[]): void {
console.log("Making a sandwich with:");
ingredients.forEach((ingredient) => {
cons
Ex_43
/*
• Unchanged Magicians: Start with your work from Exercise 40. Call the function make_great() with a copy of the array of magicians’ names. Because the original array will be unchanged, return the new array and store it in a separate array. Call show_magicians() with each array to show that you have one array of the original names and one array with the Great added to each magician’s name.
*/
function show_magicians(magicians: string[]): void {
magicians.forEach((magician) => {
co
Ex_42
/*
• Great Magicians: Start with a copy of your program from Exercise 39. Write a function called make_great() that modifies the array of magicians by adding the phrase the Great to each magician’s name. Call show_magicians() to see that the list has actually been modified.
*/
function show_magicians(magicians: string[]): void {
magicians.forEach((magician) => {
console.log(magician);
});
}
Ex_41
/*
Magicians: Make a array of magician’s names. Pass the array to a function called show_magicians(), which prints the name of each magician in the array.
*/
function show_magicians(magicians: string[]): void {
magicians.forEach((magician) => {
console.log(magician);
});
}
const magicianNames: string[] = ["David Copperfield", "Harry Houdini", "Penn Jillette", "Teller"];
Ex_40
/*
• Album: Write a function called make_album() that builds a Object describing a music album. The function should take in an artist name and an album title, and it should return a Object containing these two pieces of information. Use the function to make three dictionaries representing different albums. Print each return value to show that Objects are storing the album information correctly. Add an optional parameter to make_album() that allows you to store the number of tracks on an album.
Ex_39
/*
• City Names: Write a function called city_country() that takes in the name of a city and its country. The function should return a string formatted like this:
• "Lahore, Pakistan"
• Call your function with at least three city-country pairs, and print the value that’s returned
*/
function city_country(city: string, country: string): string {
return `${city}, ${country}`;
}
// Call the function with three city-country pairs and print the returned values
Ex_38
/*
• Cities: Write a function called describe_city() that accepts the name of a city and its country. The function should print a simple sentence, such as Karachi is in Pakistan. Give the parameter for the country a default value. Call your function for three different cities, at least one of which is not in the default country.
*/
function describe_city(city: string, country: string = "Unknown"): void {
console.log(`${city} is in ${country}.`);
}
// Call the function for three different
Ex_37
/*
function make_shirt(size: string, message: string): void {
console.log(`Shirt size: ${size}, Message: ${message}`);
}
// Call the function
make_shirt("medium", "Hello, World!");
*/
function make_shirt(size: string = "large", message: string = "I love TypeScript"): void {
console.log(`Shirt size: ${size}, Message: ${message}`);
Ex_36
/*
• T-Shirt: Write a function called make_shirt() that accepts a size and the text of a message that should be printed on the shirt. The function should print a sentence summarizing the size of the shirt and the message printed on it. Call the function.
*/
function make_shirt(size: string, message: string): void {
console.log(`Shirt size: ${size}, Message: ${message}`);
}
// Call the function
make_shirt("medium", "Hello, World!");
Ex_35
/*
• Animals: Think of at least three different animals that have a common characteristic. Store the names of these animals in a list, and then use a for loop to print out the name of each animal. • Modify your program to print a statement about each animal, such as A dog would make a great pet. • Add a line at the end of your program stating what these animals have in common. You could print a sentence such as Any of these animals would make a great pet!
*/
let animals: string[] = ["dog", "cat
Ex_34
/*
• Pizzas: Think of at least three kinds of your favorite pizza. Store these pizza names in a array, and then use a for loop to print the name of each pizza.
o Modify your for loop to print a sentence using the name of the pizza instead of printing just the name of the pizza. For each pizza you should have one line of output containing a simple statement like I like pepperoni pizza.
o Add a line at the end of your program, outside the for loop, that states how much you like pizza. The output
Ex_33
/*
• Ordinal Numbers: Ordinal numbers indicate their position in a array, such as 1st or 2nd. Most ordinal numbers end in th, except 1, 2, and 3.
o Store the numbers 1 through 9 in a array.
o Loop through the array.
o Use an if-else chain inside the loop to print the proper ordinal ending for each number. Your output should read "1st 2nd 3rd 4th 5th 6th 7th 8th 9th", and each result should be on a separate line.
*/
let numbers: number[] = [1, 2, 3, 4, 5, 6, 7, 8, 9];
for (let number of numbers
Ex_32
/*
• Checking Usernames: Do the following to create a program that simulates how websites ensure that everyone has a unique username.
o Make a list of five or more usernames called current_users.
o Make another list of five usernames called new_users. Make sure one or two of the new usernames are also in the current_users list.
o Loop through the new_users list to see if each new username has already been used. If it has, print a message that the person will need to enter a new username. If a u
Ex_31
/*
• No Users: Add an if test to Exercise 28 to make sure the list of users is not empty.
o If the list is empty, print the message We need to find some users!
o Remove all of the usernames from your array, and make sure the correct message is printed.
*/
let usernames: string[] = []; // Empty array
if (usernames.length === 0) {
console.log("We need to find some users!");
} else {
Ex_30
/*
let usernames: string[] = ["admin", "Alice", "Bob", "Charlie", "David"];
for (let username of usernames) {
if (username === "admin") {
console.log("Hello admin, would you like to see a status report?");
} else {
console.log(`Hello ${username}, thank you for logging in again.`);
}
}
Ex_29
/*
Favorite Fruit: Make a array of your favorite fruits, and then write a series of independent if statements that check for certain fruits in your array.
Make a array of your three favorite fruits and call it favorite_fruits.
Write five if statements. Each should check whether a certain kind of fruit is in your array. If the fruit is in your array, the if block should print a statement, such as You really like bananas!
*/
let favorite_fruits: string[] = ["banana", "apple", "orange"];
if (favo
Ex_28
/*
• Stages of Life: Write an if-else chain that determines a person’s stage of life. Set a value for the variable age, and then:
o If the person is less than 2 years old, print a message that the person is a baby.
o If the person is at least 2 years old but less than 4, print a message that the person is a toddler.
o If the person is at least 4 years old but less than 13, print a message that the person is a kid.
o If the person is at least 13 years old but less than 20, print a message that t
Ex_27
/*
• Alien Colors #3: Turn your if-else chain from Exercise 5-4 into an if-else chain.
o If the alien is green, print a message that the player earned 5 points.
o If the alien is yellow, print a message that the player earned 10 points.
o If the alien is red, print a message that the player earned 15 points.
o Write three versions of this program, making sure each message is printed for the appropriate color alien.
*/
let alien_color: string = 'yellow';
if (alien_color === 'green') {
Ex_26
/*
• Alien Colors #2: Choose a color for an alien as you did in Exercise 25, and write an if-else chain.
o If the alien’s color is green, print a statement that the player just earned 5 points for shooting the alien.
o If the alien’s color isn’t green, print a statement that the player just earned 10 points.
o Write one version of this program that runs the if block and another that runs the else block.
*/
let alien_color: string = 'green';
if (alien_color === 'green') {
console.log("Playe