# 🏅 Week1 - typescript 練習 **本週學習重點:** 1. 基本型別 number, string, boolean, undefined, null, 2. 任意值 any 3. 型別推論 type infer 4. 聯合型別 5. interface 介面/接口 ( 可選屬性/任意屬性/唯讀屬性 ) 6. array 數組型別 10. 函式型別 ( 陳述/表達/可選參數/參數默認/重載/剩餘參數 ) <br> --- ### 練習:typescript 本週重點觀念習題 **題目一:** (基本型別) Create a variable called highScore that can be a number OR a boolean **題目二:** (array 數組型別 & 聯合型別) create an array called stuff it can be an array of numbers OR an array of strings it cannot be an array of numbers and strings (mixed together) **題目三** ( 函式型別 ) Write a function called greet that accepts a single string OR an array of strings It should print "Hello, <name>" for that single person OR greet each person in the array with the same format **題目四** ( interface ) Given this interface: ``` javaSctipt interface Cryable { cry(): string; } ``` Select the answer below that is a valid implementation of the Cryable interface ``` javaSctipt // option A const human: Cryable = { cry(person:string) { return "BOO HOO" } } ``` ``` javaSctipt // option B const human: Cryable = { cry() { console.log("BOO HOO") } } ``` ``` javaSctipt // option C const human: Cryable = () => { return "BOO HOO" } ``` ``` javaSctipt // option D const human: Cryable = { cry() { return "BOO HOO" } } ``` **題目五** ( interface ) Which of the following Addable interfaces uses INVALID TypeScript syntax: ``` javaSctipt // option A interface Addable { add(x:number, y:number):number; } ``` ``` javaSctipt // option B interface Addable { add:(x:number, y:number) => number; } ``` ``` javaSctipt // option C interface Addable { add(x:number, y:number) { number; } } ``` **題目六** ( function ) Given the following function definition: ``` javaSctipt function multiply(x,y) { return x*y } ``` What are the TypeScript types of the parameters x and y ? A. number B. any C. unkown D. undefined **題目七** ( function ) Given the following secondsInDay function: ``` javaSctipt function secondsInDay() { return 24 * 60 * 60; } ``` What return type does TypeScript assign to it? A. Error: missing required return annotation B. any C. number D. undefined **題目八** ( function ) What return type does TypeScript infer for the following function: ``` javaSctipt function doNothing() { 2 + 2; } ``` A. number B. undefined C. any D. void **題目九** ( function ) Write a function called "twoFer" that accepts a person's name It should return a string in the format "one for <name>, one for me" If no name is provided, it should default to "you" ``` javaSctipt twoFer() => "One for you, one for me" twoFer("Elton") => "One for Elton, one for me" ``` **題目十** ( function ) Write a isLeapyear() function that accepts a year and returns true/false depending on if the year is a leap year ``` javaSctipt isLeapYear(2012) => true isLeapYear(2013) => false ``` To determine whether a year is a leapyear, use this "formula": A YEAR IS A LEAPYEAR IF - year is a multiple of 4 AND not a multiple of 100 OR... - year is a multiple of 400 hint - use modulo <!-- 答案網址: https://codepen.io/bruno-yu/pen/dyaqNjm?editors=0010 --> | 名稱 | 回報網址 | | -------- | :--------: | | Bruno | [codepen](https://codepen.io/bruno-yu/pen/YzBONxe?editors=0010) | | 真真 | [codepen](https://codepen.io/JudithCrocodile/pen/qBgLNVd?editors=1010) | | 家欣 | [codepen](https://codepen.io/scwlvjdw-the-bashful/pen/MWLzPGV?editors=0011) | | | | || | ---