Quiz 3 TypeScript Basics

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:

  1. Typing an Object

    Create a TypeScript interface or type named Book to describe the structure of the objects within the books array.

  2. Copy Elements

    Copy the last 2 objects from the array books, and console.log the result.

  3. 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};, and console.log the updated books.

    let a = { title: "The Hobbit", author: "J.R.R. Tolkien", pages: 310 };
    let b = { title: "Pride and Prejudice", author: "Jane Austen", pages: 432 };
    
  4. Iterate Over Object Properties

    Use for-loop to console.log all properties names and values of all objects in books.

  5. Splitting an Array into Two Parts with Destructuring and the Spread Operator

    Write a function splitIt that takes any Book 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: