3. Quiz 3 TypeScript Basics#
Given the following array,
let books = [
{ title: "1984", author: "George Orwell", pages: 328 },
{ title: "The Great Gatsby", author: "F. Scott Fitzgerald", pages: 180 },
{ title: "To Kill a Mockingbird", author: "Harper Lee", pages: 281 },
{ title: "Brave New World", author: "Aldous Huxley", pages: 264 },
{ title: "The Catcher in the Rye", author: "J.D. Salinger", pages: 234 },
];
Complete the following tasks in TypeScript:
Typing an Object
Create a TypeScript
interface
ortype
namedBook
to describe the structure of the objects within thebooks
array.Copy Elements
Copy the last 2 objects from the array
books
, andconsole.log
the result.Replace Elements
In the books array, replace the 3rd object with two new objects
{ title: "The Hobbit", author: "J.R.R. Tolkien", pages: 310}
and{title: "Pride and rejudice", author: "Jane Austen", pages: 432};
, andconsole.log
the updatedbooks
.let a = { title: "The Hobbit", author: "J.R.R. Tolkien", pages: 310 }; let b = { title: "Pride and Prejudice", author: "Jane Austen", pages: 432 };
Iterate Over Object Properties
Use for-loop to
console.log
all properties names and values of all objects inbooks
.Splitting an Array into Two Parts with Destructuring and the Spread Operator
Write a function
splitIt
that takes anyBook
array as input, splits it into two parts: the first two elements and the rest, and returns both parts. Test your function using code below.const [firstTwo, theRest] = splitIt(books); console.log("First Two Books:", firstTwo); console.log("The Rest of the Books:", theRest);
Test your solutions on typescriptlang.org: