Last active
May 6, 2024 16:40
-
-
Save QuocCao-dev/1b5d5e7e960ddfadcc75687fdcbac241 to your computer and use it in GitHub Desktop.
Typescript Exercises
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* *-*-*-*-*-*-*-*-*-*-* Challenge 1 ------------------ | |
Create a variable with the type number and assign it an arbitrary value | |
*/ | |
// ---------------------------------------------------- | |
// *-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*- | |
// ---------------------------------------------------- | |
/* *-*-*-*-*-*-*-*-*-*-* Challenge 2 ------------------ | |
Create a variable with the type string and use the addition operator to put two arbitrary words together | |
*/ | |
// ---------------------------------------------------- | |
// *-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*- | |
// ---------------------------------------------------- | |
/* *-*-*-*-*-*-*-*-*-*-* Challenge 3 ------------------ | |
Simulate a Type Error by assigning a number to a string | |
*/ | |
// ---------------------------------------------------- | |
// *-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*- | |
// ---------------------------------------------------- | |
/* *-*-*-*-*-*-*-*-*-*-* Challenge 4 ------------------ | |
Create a variable with the type Boolean and assign it to the logical OR operator comparing two values | |
*/ | |
// ---------------------------------------------------- | |
// *-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*- | |
// ---------------------------------------------------- | |
/* *-*-*-*-*-*-*-*-*-*-* Challenge 5 ------------------ | |
Simulate a Type Error by assigning a number to a boolean | |
*/ | |
// ---------------------------------------------------- | |
// *-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*- | |
// ---------------------------------------------------- | |
/* *-*-*-*-*-*-*-*-*-*-* Challenge 6 ------------------ | |
Get the length of an arbitrary string | |
*/ | |
SOLUTION: | |
// Challenge 1 ----------------------------------------------- | |
// let score: number = 19; | |
// | |
// Challenge 2 ----------------------------------------------- | |
// let myName: string = "Muslim" + " " + "Helalee"; | |
// *-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-* | |
// *-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-* | |
// Challenge 3 ----------------------------------------------- | |
// let size: string = 15 | |
// *-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-* | |
// *-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-* | |
// Challenge 4 ----------------------------------------------- | |
// let passed: boolean = true || false; | |
// *-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-* | |
// *-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-* | |
// Challenge 5 ----------------------------------------------- | |
// let failed: boolean = 15 | |
// *-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-* | |
// *-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-* | |
// Challenge 6 ----------------------------------------------- | |
// let language: number = "TypeScript".length; | |
// *-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-* | |
// *-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-* | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* *-*-*-*-*-*-*-*-*-*-* Challenge 1 ------------------ | |
What will the following function invocation result in? | |
*/ | |
// function courseName(): string { | |
// return "TS"; | |
// } | |
// let typeScript: "TS" = courseName(); | |
// ---------------------------------------------------- | |
// *-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*- | |
// ---------------------------------------------------- | |
/* *-*-*-*-*-*-*-*-*-*-* Challenge 2 ------------------ | |
What will accessing the variable favBook result be? | |
*/ | |
// type BookType = | |
// | "And the Mountains Echoed" | |
// | "The Kite Runner" | |
// | "A Thousand Splendid Suns"; | |
// let favBook: BookType = "The Kite Runner"; | |
// favBook; | |
// ---------------------------------------------------- | |
// *-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*- | |
// ---------------------------------------------------- | |
/* *-*-*-*-*-*-*-*-*-*-* Challenge 3 ------------------ | |
What will the following code result in? | |
*/ | |
// type BookType = | |
// | "And the Mountains Echoed" | |
// | "The Kite Runner" | |
// | "A Thousand Splendid Suns"; | |
// let favBook: BookType = "The Shining"; | |
// favBook; | |
// ---------------------------------------------------- | |
// *-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*- | |
// ---------------------------------------------------- | |
/* *-*-*-*-*-*-*-*-*-*-* Challenge 4 ------------------ | |
What will the following variable assignment result in? | |
*/ | |
// let bool: true = false; | |
// ---------------------------------------------------- | |
// *-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*- | |
// ---------------------------------------------------- |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* *-*-*-*-*-*-*-*-*-*-* Challenge 1 ------------------ | |
What will the following function invocation result in? | |
*/ | |
// function courseName(): string { | |
// return "TS"; | |
// } | |
// let typeScript: "TS" = courseName(); | |
// ---------------------------------------------------- | |
// *-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*- | |
// ---------------------------------------------------- | |
/* *-*-*-*-*-*-*-*-*-*-* Challenge 2 ------------------ | |
What will accessing the variable favBook result be? | |
*/ | |
// type BookType = | |
// | "And the Mountains Echoed" | |
// | "The Kite Runner" | |
// | "A Thousand Splendid Suns"; | |
// let favBook: BookType = "The Kite Runner"; | |
// favBook; | |
// ---------------------------------------------------- | |
// *-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*- | |
// ---------------------------------------------------- | |
/* *-*-*-*-*-*-*-*-*-*-* Challenge 3 ------------------ | |
What will the following code result in? | |
*/ | |
// type BookType = | |
// | "And the Mountains Echoed" | |
// | "The Kite Runner" | |
// | "A Thousand Splendid Suns"; | |
// let favBook: BookType = "The Shining"; | |
// favBook; | |
// ---------------------------------------------------- | |
// *-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*- | |
// ---------------------------------------------------- | |
/* *-*-*-*-*-*-*-*-*-*-* Challenge 4 ------------------ | |
What will the following variable assignment result in? | |
*/ | |
// let bool: true = false; | |
// ---------------------------------------------------- | |
// *-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*- | |
// ---------------------------------------------------- |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* *-*-*-*-*-*-*-*-*-*-* Challenge 1 ------------------ | |
Create an object type that has 3 properties (name, age, job) and create an object from this type | |
*/ | |
// ---------------------------------------------------- | |
// *-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*- | |
// ---------------------------------------------------- | |
/* *-*-*-*-*-*-*-*-*-*-* Challenge 2 ------------------ | |
Access the published property from the following object using the bracket notation and the dot notation | |
*/ | |
// type OnlineCourse = { | |
// name: string; | |
// platform: string; | |
// features: string[]; | |
// "meta-data": { | |
// published: boolean; | |
// }; | |
// }; | |
// let myCourse: OnlineCourse = { | |
// name: "TS Bootcamp", | |
// platform: "Udemy", | |
// features: ["Practical", "Exercise Oriented", "Modern TS Concepts"], | |
// "meta-data": { | |
// published: true, | |
// }, | |
// }; | |
// ---------------------------------------------------- | |
// *-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*- | |
// ---------------------------------------------------- | |
/* *-*-*-*-*-*-*-*-*-*-* Challenge 3 ------------------ | |
Fix the error in the following code by modifying the type Product. Do not make changes to the product1 or product2 objects | |
*/ | |
// type Product = { | |
// name: string; | |
// publishedStatus: boolean; | |
// }; | |
// const product1: Product = { name: "TS Bootcamp" }; | |
// const product2: Product = { name: "TS Bootcamp", publishedStatus: true }; | |
// ---------------------------------------------------- | |
// *-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*- | |
// ---------------------------------------------------- | |
/* *-*-*-*-*-*-*-*-*-*-* Challenge 4 ------------------ | |
What will the product object log? | |
*/ | |
// const onlineCourse = { name: "TS Bootcamp", price: 10.99 }; | |
// const product = { ...onlineCourse, released: true }; | |
// console.log(product); | |
// ---------------------------------------------------- | |
// *-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*- | |
// ---------------------------------------------------- | |
/* *-*-*-*-*-*-*-*-*-*-* Challenge 5 ------------------ | |
Create a product type and add a holidaySales method that return a string “Sale is On”. Finish by creating an object from the type and logging the string on the console | |
*/ | |
// ---------------------------------------------------- | |
// *-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*- | |
// ---------------------------------------------------- |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* *-*-*-*-*-*-*-*-*-*-* Challenge 1 ------------------ | |
Write a function that takes a literal object with one property of name and simply returns the name | |
*/ | |
// ---------------------------------------------------- | |
// *-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*- | |
// ---------------------------------------------------- | |
/* *-*-*-*-*-*-*-*-*-*-* Challenge 2 ------------------ | |
Write a function that takes an object type with one property of online (type boolean) as argument and simply returns the whether the user is online or offline | |
*/ | |
// ---------------------------------------------------- | |
// *-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*- | |
// ---------------------------------------------------- |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* *-*-*-*-*-*-*-*-*-*-* Challenge 1 ------------------ | |
What is the result of the following variable assignment? | |
*/ | |
/* Do not comment in unless answered | |
let vague: number | undefined = null | |
*/ | |
// ---------------------------------------------------- | |
// *-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*- | |
// ---------------------------------------------------- | |
/* *-*-*-*-*-*-*-*-*-*-* Challenge 2 ------------------ | |
What is the result of the second variable assignment? | |
*/ | |
/* Do not comment in unless answered | |
let vague1: boolean | undefined = true ? true : undefined; | |
let vague2: boolean = vague1; | |
*/ | |
// ---------------------------------------------------- | |
// *-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*- | |
// ---------------------------------------------------- | |
/* *-*-*-*-*-*-*-*-*-*-* Challenge 3 ------------------ | |
What is the result of the following variable assignment? | |
*/ | |
/* Do not comment in unless answered | |
let vague: string | undefined = undefined; | |
*/ | |
// ---------------------------------------------------- | |
// *-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*- | |
// ---------------------------------------------------- | |
/* *-*-*-*-*-*-*-*-*-*-* Challenge 4 ------------------ | |
What is the value of the vague2 variable? Make sure to check for both boolean and undefined values | |
*/ | |
/* Do not comment in unless answered | |
let vague1: boolean | undefined = true ? true : undefined; | |
// let vague1: boolean | undefined = true ? undefined : true; | |
let vague2: boolean; | |
if (vague1 === undefined) { | |
vague2 = false; | |
} else { | |
vague2 = true; | |
} | |
console.log(vague2); | |
*/ | |
// ---------------------------------------------------- | |
// *-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*- | |
// ---------------------------------------------------- | |
/* *-*-*-*-*-*-*-*-*-*-* Challenge 5 ------------------ | |
Create a function that produces a value based on the following conditions | |
1--------- If the argument passed is a string containing a number, it should return the number | |
2---------If the argument passed is a string not containing a number, it should return undefined | |
3---------If the argument passed is undefined, it should return undefined | |
*/ | |
// ---------------------------------------------------- | |
// *-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*- | |
// ---------------------------------------------------- |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* *-*-*-*-*-*-*-*-*-*-* Challenge 1 ------------------ | |
Assign a number to a variable with no type annotation, what is the variable type inferred by TypeScript? | |
*/ | |
// ---------------------------------------------------- | |
// *-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*- | |
// ---------------------------------------------------- | |
/* *-*-*-*-*-*-*-*-*-*-* Challenge 2 ------------------ | |
Assign a string to a variable with no type annotation, what is the variable type inferred by TypeScript? | |
*/ | |
// ---------------------------------------------------- | |
// *-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*- | |
// ---------------------------------------------------- | |
/* *-*-*-*-*-*-*-*-*-*-* Challenge 3 ------------------ | |
Assign a boolean false to a variable with no type annotation, what is the variable type inferred by TypeScript? | |
*/ | |
SOLUTION: | |
// Challenge 1 ----------------------------------------------- | |
// let score = 31 // number | |
// *-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-* | |
// *-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-* | |
// Challenge 2 ----------------------------------------------- | |
// let book = "And the Mountains Echoed" // string | |
// *-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-* | |
// *-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-* | |
// Challenge 3 ----------------------------------------------- | |
// let passed = false // boolean | |
// *-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-* | |
// *-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-* |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* *-*-*-*-*-*-*-*-*-*-* Challenge 1 ------------------ | |
Create an array of numbers | |
*/ | |
// ---------------------------------------------------- | |
// *-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*- | |
// ---------------------------------------------------- | |
/* *-*-*-*-*-*-*-*-*-*-* Challenge 2 ------------------ | |
From the previously create array, access the second-indexed item | |
*/ | |
// ---------------------------------------------------- | |
// *-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*- | |
// ---------------------------------------------------- | |
/* *-*-*-*-*-*-*-*-*-*-* Challenge 3 ------------------ | |
Create an array of the two boolean values | |
*/ | |
// ---------------------------------------------------- | |
// *-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*- | |
// ---------------------------------------------------- | |
SOLUTION: | |
// Challenge 1 ----------------------------------------------- | |
// let numbers: number[] = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]; | |
// *-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-* | |
// *-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-* | |
// Challenge 2 ----------------------------------------------- | |
// console.log(numbers[2]); | |
// *-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-* | |
// *-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-* | |
// Challenge 3 ----------------------------------------------- | |
// let myArr1: boolean[] = [true, false]; | |
// *-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-* | |
// *-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-* |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* *-*-*-*-*-*-*-*-*-*-* Challenge 1 ------------------ | |
Create a string type tuple | |
*/ | |
// ---------------------------------------------------- | |
// *-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*- | |
// ---------------------------------------------------- | |
/* *-*-*-*-*-*-*-*-*-*-* Challenge 2 ------------------ | |
Create a number type tuple | |
*/ | |
// ---------------------------------------------------- | |
// *-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*- | |
// ---------------------------------------------------- | |
/* *-*-*-*-*-*-*-*-*-*-* Challenge 3 ------------------ | |
Create a string and number type tuple and access the second item | |
*/ | |
// ---------------------------------------------------- | |
// *-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*- | |
// ---------------------------------------------------- | |
SOLUTION: | |
// Challenge 1 ----------------------------------------------- | |
// let myTuple1: [string] = ["TypeScript"]; | |
// *-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-* | |
// *-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-* | |
// Challenge 2 ----------------------------------------------- | |
// let myTuple2: [number] = [35]; | |
// *-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-* | |
// *-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-* | |
// Challenge 3 ----------------------------------------------- | |
// let myTuple3: [string, number] = ["false", 35]; | |
// console.log(myTuple3[1]); | |
// *-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-* | |
// *-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-* |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* *-*-*-*-*-*-*-*-*-*-* Challenge 1 ------------------ | |
Create an enum of product names and display it on the console | |
*/ | |
// ---------------------------------------------------- | |
// *-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*- | |
// ---------------------------------------------------- | |
/* *-*-*-*-*-*-*-*-*-*-* Challenge 2 ------------------ | |
Create an enum of product ids and display it on the console | |
*/ | |
// ---------------------------------------------------- | |
// *-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*- | |
// ---------------------------------------------------- | |
/* *-*-*-*-*-*-*-*-*-*-* Challenge 3 ------------------ | |
Create an enum of a single favorite product detail from the two previously created enums and display it on the console | |
*/ | |
SOLUTION: | |
// Challenge 1 ----------------------------------------------- | |
// enum ProductNames { | |
// product1 = "Laptop", | |
// product2 = "Mouse", | |
// product3 = "Keyboard", | |
// } | |
// console.log(ProductNames); | |
// *-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-* | |
// *-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-* | |
// Challenge 2 ----------------------------------------------- | |
// enum ProductIds { | |
// Laptop = 1, | |
// Mouse, | |
// Keyboard, | |
// } | |
// console.log(ProductIds); | |
// *-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-* | |
// *-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-* | |
// Challenge 3 ----------------------------------------------- | |
// enum FavoriteProduct { | |
// Name = ProductNames.product1, | |
// Id = ProductIds.Laptop, | |
// } | |
// console.log( | |
// `My favorite product is ${FavoriteProduct.Name} with an id of ${FavoriteProduct.Id}` | |
// ); | |
// *-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-* | |
// *-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-* | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* *-*-*-*-*-*-*-*-*-*-* Challenge 1 ------------------ | |
Based on the below array of search history of a user, return “Would you like to learn how to create websites for free?” if there is a keyword html or css | |
*/ | |
let searchHistory: string[] = [ | |
"how to make money online", | |
"benefits of eating garlic", | |
"html css tutorial", | |
"calisthenics", | |
]; | |
// ---------------------------------------------------- | |
// *-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*- | |
// ---------------------------------------------------- | |
SOLUTION: | |
// Challenge ----------------------------------------------- | |
// Long Version | |
// for (let searchItem of searchHistory) { | |
// // console.log(searchItem.split(" ")); | |
// for (let entry of searchItem.split(" ")) { | |
// // console.log(entry); | |
// if (entry === "html" || entry === "css") | |
// console.log("Would you like to learn how to create websites for free?"); | |
// } | |
// } | |
// Short version | |
// searchHistory.map((entry) => { | |
// if (entry.includes("html") || entry.includes("css")) | |
// console.log("Would you like to learn how to create websites for free?"); | |
// }); | |
// *-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-* | |
// *-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-* |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* *-*-*-*-*-*-*-*-*-*-* Challenge 1 ------------------ | |
Create a function that returns the multiplication of two arbitrary numbers and returns a type error when passed a string | |
*/ | |
// ---------------------------------------------------- | |
// *-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*- | |
// ---------------------------------------------------- | |
/* *-*-*-*-*-*-*-*-*-*-* Challenge 2 ------------------ | |
Simulate a type error for the returned value of a function of arbitrary type | |
*/ | |
// ---------------------------------------------------- | |
// *-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*- | |
// --------------------------------------------------- | |
/* *-*-*-*-*-*-*-*-*-*-* Challenge 3 ------------------ | |
Create an arrow function that returns the addition of a number and a boolean type | |
*/ | |
// ---------------------------------------------------- | |
// *-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*- | |
// ---------------------------------------------------- | |
/* *-*-*-*-*-*-*-*-*-*-* Challenge 4 ------------------ | |
Create a function that makes words plural by adding "s" | |
*/ | |
// ---------------------------------------------------- | |
// *-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*- | |
// ---------------------------------------------------- | |
/* *-*-*-*-*-*-*-*-*-*-* Challenge 5 ------------------ | |
Write a function that adds or subtracts 1 from a number based on a boolean evaluation. If the boolean is true, it adds 1 otherwise subtracts 1 from the number | |
*/ | |
// ---------------------------------------------------- | |
// *-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*- | |
// ---------------------------------------------------- | |
/* *-*-*-*-*-*-*-*-*-*-* Challenge 6 ------------------ | |
Create a function that takes an array of numbers, then returns it. Does not change the array. If a string is passed, it throws a type error | |
*/ | |
// ---------------------------------------------------- | |
// *-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*- | |
// ---------------------------------------------------- | |
/* *-*-*-*-*-*-*-*-*-*-* Challenge 7 ------------------ | |
Create a function that takes an array of tuples consisting of two properties, a name and an age. Return the names in a string array | |
*/ | |
// ---------------------------------------------------- | |
// *-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*- | |
// ---------------------------------------------------- | |
SOLUTION: | |
// Challenge 1 ----------------------------------------------- | |
// function multiplication(num1: number, num2: number): number { | |
// return num1 * num2; | |
// } | |
// const result = multiplication(1, 5); | |
// const result = multiplication(115, 5); | |
// const result = multiplication(115, "5"); | |
// console.log(result); | |
// *-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-* | |
// *-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-* | |
// Challenge 2 ----------------------------------------------- | |
// function emailList(email: string): string { | |
// return email; | |
// } | |
// emailList(15); | |
// *-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-* | |
// *-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-* | |
// Challenge 3 ----------------------------------------------- | |
// let add = (num: number, bool: boolean): number => num + bool; | |
// *-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-* | |
// *-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-* | |
// Challenge 4 ----------------------------------------------- | |
// let pluralize = function (str: string): string { | |
// return str + "s"; | |
// }; | |
// console.log(pluralize("cake")); | |
// *-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-* | |
// *-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-* | |
// Challenge 5 ----------------------------------------------- | |
// Long version | |
// function decideOperation(num: number, bool: boolean): number { | |
// if (bool) return num + 1; | |
// return num - 1; | |
// } | |
// Short version | |
// let decideOperation = (num: number, bool: boolean): number => | |
// bool ? num + 1 : num - 1; | |
// let operationResult = decideOperation(10, true); | |
// let operationResult = decideOperation(10, false); | |
// console.log(operationResult); | |
// *-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-* | |
// *-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-* | |
// Challenge 6 ----------------------------------------------- | |
// function myArray(arr: number[]): number[] { | |
// return arr; | |
// } | |
// let result = myArray([1, 2, 3]); | |
// let result = myArray([1, "2", 3]); | |
// console.log(result); | |
// *-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-* | |
// *-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-* | |
// Challenge 7 ----------------------------------------------- | |
// function getNames(data: [string, number][]): string[] { | |
// return data.map((name) => name[0]); | |
// } | |
// console.log( | |
// getNames([ | |
// ["John", 35], | |
// ["Jane", 45], | |
// ["Jade", 55], | |
// ]) | |
// ); | |
// *-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-* | |
// *-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-* | |
// function productId(): string | number { | |
// return 112233; | |
// } | |
// let id: number = productId(); | |
// id; | |
// ---------------------------------------------------- | |
// *-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*- | |
// ---------------------------------------------------- | |
/* *-*-*-*-*-*-*-*-*-*-* Challenge 3 ------------------ | |
What will the function invocation result in? | |
*/ | |
// function productId(): number { | |
// return 112233; | |
// } | |
// let id: string | number = productId(); | |
// id; | |
// ---------------------------------------------------- | |
// *-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*- | |
// ---------------------------------------------------- | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* *-*-*-*-*-*-*-*-*-*-* Challenge 1 ------------------ | |
What will the function invocation result in? | |
*/ | |
// function productId(): string | number { | |
// return 112233; | |
// } | |
// let id: string | number = productId(); | |
// id; | |
// ---------------------------------------------------- | |
// *-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*- | |
// ---------------------------------------------------- | |
/* *-*-*-*-*-*-*-*-*-*-* Challenge 2 ------------------ | |
What will the function invocation result in? | |
*/ | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* *-*-*-*-*-*-*-*-*-*-* Challenge 1 ------------------ | |
Create a custom type and assign it both boolean values | |
*/ | |
// ---------------------------------------------------- | |
// *-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*- | |
// ---------------------------------------------------- | |
/* *-*-*-*-*-*-*-*-*-*-* Challenge 2 ------------------ | |
What will be the value of variable score? | |
*/ | |
// type Score = number; | |
// let avgScore: string = "11.5"; | |
// let score: Score = avgScore; | |
// ---------------------------------------------------- | |
// *-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*- | |
// ---------------------------------------------------- | |
/* *-*-*-*-*-*-*-*-*-*-* Challenge 3 ------------------ | |
What will be the value of variable score? | |
*/ | |
// type Score = number; | |
// let avgScore: number = 11.5; | |
// let score: Score = avgScore; | |
// ---------------------------------------------------- | |
// *-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*- | |
// ---------------------------------------------------- |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment