R
@Raza74
Ex_25
/*
• Alien Colors #1: Imagine an alien was just shot down in a game. Create a variable called alien_color and assign it a value of 'green', 'yellow', or 'red'.
o Write an if statement to test whether the alien’s color is green. If it is, print a message that the player just earned 5 points.
o Write one version of this program that passes the if test and another that fails. (The version that fails will have no output.)
*/
let alien_color: string = 'green';
if (alien_color === 'green') {
con
Ex-24
/*
• More Conditional Tests: You don’t have to limit the number of tests you create to 10. If you want to try more comparisons, write more tests. Have at least one True and one False result for each of the following:
o Tests for equality and inequality with strings
o Tests using the lower case function
o Numerical tests involving equality and inequality, greater than and less than, greater than or equal to, and less than or equal to
o Tests using "and" and "or" operators
o Test whether an item
Extra 16
let guestList = ["hammad", "usman", "kashan", "aliyan"];
let dontCome = guestList[0];
console.log(dontCome, "sorry i m bussy");
guestList.splice(0, 1, "yousuf");
console.log("good news we have find a bigger tabel for a dinner");
Ex_23
/*
• Conditional Tests: Write a series of conditional tests. Print a statement describing each test and your prediction for the results of each test. Your code should look something like this:
• let car = 'subaru';
• console.log("Is car == 'subaru'? I predict True.")
• console.log(car == 'subaru')
o Look closely at your results, and make sure you understand why each line evaluates to True or False.
o Create at least 10 tests. Have at least 5 tests evaluate to True and another 5 tests evaluate t
Ex_22
/*
Intentional Error: If you haven’t received an array index error in one of your programs yet, try to make one happen. Change an index in one of your programs to produce an index error. Make sure you correct the error before closing the program.
*/
let array: number[] = [1, 2, 3, 4, 5];
let index = 10; // Intentionally setting an index that is out of bounds
try {
let element = array[index]; // Accessing an element with an out-of-bounds index
console.log("Element at index", index, ":"
Ex_21
/*
21. They think of something you could store in a TypeScript Object. Write a program that creates Objects containing these items.
*/
// Define types for the items
interface Item {
name: string;
category: string;
quantity: number;
}
Ex_20
/*
20. Think of something you could store in a array. For example, you could make a list of mountains, rivers, countries, cities, languages, or anything else you’d like. Write a program that creates a list containing these items.
*/
// Define an array of mountains
let mountains: string[] = ["Mount Everest", "K2", "Kgchenjunga", "Lhotse", "Makalu"];
// Print the list of mountains
console.log("List of mountains:");
mountains.forEach((mountain, index) => {
Ex_19
/*
Dinner Guests: Working with one of the programs from Exercises 14 through 18, print a message indicating the number of people you are inviting to dinner.
*/
interface Person { name: string;}
function countDinnerGuests(people: Person[]): void {
Ex_18
/*
Seeing the World: Think of at least five places in the world you’d like to visit.
Store the locations in a array. Make sure the array is not in alphabetical order.
Print your array in its original order.
Print your array in alphabetical order without modifying the actual list.
Show that your array is still in its original order by printing it.
Print your array in reverse alphabetical order without changing the order of the original list.
Show that your array is still in its original order by
Ex_17
/*
Shrinking Guest List: You just found out that your new dinner table won’t arrive in time for the dinner, and you have space for only two guests.
Start with your program from Exercise 16.
Add a new line that prints a message saying that you can invite only two people for dinner.
Remove guests from your list one at a time until only two names remain in your list.
Each time you pop a name from your list, print a message to that person letting them know you’re sorry you can’t invite them to di
Ex_16
/*
More Guests: You just found a bigger dinner table, so now more space is available. Think of three more guests to invite to dinner.
Start with your program from Exercise 15. Add a print statement to the end of your program informing people that you found a bigger dinner table.
Add one new guest to the beginning of your array.
Add one new guest to the middle of your array.
Use append() to add one new guest to the end of your list.
Print a new set of invitation messages, one for each person i
Ex_15
/*
You just heard that one of your guests can’t make the dinner, so you need to send out a new set of invitations. You’ll have to think of someone else to invite.
Start with your program from Exercise 14. Add a print statement at the end of your program stating the name of the guest who can’t make it.
Modify your list, replacing the name of the guest who can’t make it with the name of the new person you are inviting.
Print a second set of invitation messages, one for each person who is still in
Ex_14
/*
Guest List: If you could invite anyone, living or deceased, to dinner, who would you invite? Make a list that includes at least three people you’d like to invite to dinner. Then use your list to print a message to each person, inviting them to dinner.
*/
const myfriends: string[]=["Asim","Faheem","Azeem",];
// 1st method with loop
for(let i = 0; i < myfriends.length; i++){
console.log(`Mr. ${myfriends[i]},! You are invited to my dinner on monday. `);
Ex_13
/*
Your Own Array: Think of your favorite mode of transportation, such as a motorcycle or a car, and make a list that stores several examples. Use your list to print a series of statements about these items, such as “I would like to own a Honda motorcycle.”
*/
const mytrans: string[] = ["car", "motorcycle", "bicycle", "train"];
// Using a for loop
for (let i = 0; i <= 3; i++)
console.log(`I would like to own a ${mytrans[i]}`);
Ex_12
/*
Greetings: Start with the array you used in Exercise 11, but instead of just printing each person’s name, print a message to them. The text of each message should be the same, but each message should be personalized with the person’s name.
*/
let friendName: string[] = ["Raza", "Faheem", "Azeem", "Saleem"];
console.log(`Hello ${friendName[0]}, how are you?`);
console.log(`Hello ${friendName[1]}, how are you?`);
console.log(`Hello ${friendName[2]}, how are you?`);
console.log(`Hello ${frien
Ex_11
/*
Names: Store the names of a few of your friends in a array called names. Print each person’s name by accessing each element in the list, one at a time.
*/
let friendName: string[] = ["Raza", "Faheem", "Azeem", "Saleem"];
console.log(friendName[0]);
console.log(friendName[1]);
console.log(friendName[2]);
Ex_10
/*
10. Adding Comments: Choose two of the programs you’ve written, and add at least one comment to each. If you don’t have anything specific to write because your programs are too simple at this point, just add your name and the current date at the top of each program file. Then write one sentence describing what the program does.
*/
const num1: number = 23;
const num2: number = 3;
// this will add the num1 and num2(this comment in typescript)
console.log(num1 + num2);
Ex_09
/*
Favorite Number: Store your favorite number in a variable. Then, using that variable, create a message that reveals your favorite number. Print that message.
*/
const favoriteNumber: number = 12;
console.log(`My favorite number is: ${favoriteNumber}`);
Ex_08
/*
You should create four lines that look like this:
console.log(5 + 3)
Your output should simply be four lines with the number 8 appearing once on each line.
*/
console.log(8);
Ex_07
/*
Number Eight: Write addition, subtraction, multiplication, and division operations that each result in the number 8. Be sure to enclose your operations in print statements to see the results.
*/
const addition: number = 4 + 4;
const subtraction: number = 10 - 4;
const multiplication: number = 2 * 4;
const division: number = 32 / 4;
console.log(addition,subtraction,multiplication,division);