This file contains hidden or 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
| // Init TODO type. | |
| type Todo = { | |
| readonly title: string | |
| done: boolean | |
| } | |
| // init list of TODOs. | |
| const todos: Todo[] = [ | |
| { title: 'Laundry', done: false }, |
This file contains hidden or 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
| type Todo = { | |
| readonly title: string | |
| done: boolean | |
| } | |
| const todos: Todo[] = [ | |
| { title: 'Laundry', done: false }, | |
| { title: 'Dishes', done: true} |
This file contains hidden or 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
| let bookHeights: number[] = [12, 8, 14, 9]; | |
| for (let i = 0; i < bookHeights.length - 1; i++) { | |
| let swapHappen = false; | |
| for (let shelfSpot = 0; shelfSpot < bookHeights.length-i- 1; shelfSpot++) { | |
| if (bookHeights[shelfSpot] > bookHeights[shelfSpot + 1]) { | |
| let tallerBook = bookHeights[shelfSpot]; | |
| bookHeights[shelfSpot] = bookHeights[shelfSpot + 1]; | |
| bookHeights[shelfSpot + 1] = tallerBook; | |
| swapHappen = true; |
This file contains hidden or 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
| // The class voted on a pet. Tally the ballots. | |
| let classPetVotes: string[] = ["cat", "dog", "cat", "bird", "cat", "dog"]; | |
| let voteCounts: { [animal: string]: number } = { | |
| cat: 0, | |
| dog: 0, | |
| bird: 0, | |
| fish: 0 | |
| }; | |
This file contains hidden or 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
| let runningOrder: string[] = ["Ana", "Ben", "Cruz", "Dee", "Eli"]; | |
| let frontIndex: number = 0; | |
| let backIndex: number = runningOrder.length - 2; | |
| while (frontIndex < backIndex) { | |
| let placeholder = runningOrder[frontIndex]; | |
| runningOrder[frontIndex] = runningOrder[backIndex]; | |
| runningOrder[backIndex] = placeholder; | |
| frontIndex = frontIndex + 1; | |
| backIndex = backIndex - 1; | |
| } |
This file contains hidden or 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
| function findLastPosition(waitlist: string[], personName: string): number { | |
| for (let i = waitlist.length-1; i>=0 ; i--) { | |
| if (waitlist[i] === personName) { | |
| return i; | |
| } | |
| } | |
| return -1; | |
| } | |
| let tonightsWaitlist = ["Priya", "Sam", "Jordan", "Sam", "Lee"]; | |
| console.log(findLastPosition(tonightsWaitlist, "Sam")); |
This file contains hidden or 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
| let limit:number = 100; | |
| let count:number = 0; | |
| for(let i = 1; i<=limit; i++){ | |
| if (i%7===0) count ++ | |
| } | |
| console.log(count) |
This file contains hidden or 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
| let sumlimit:number = 100; | |
| let total:number = 0; | |
| for(let i = 1; i<=sumlimit; i++){ | |
| if (i%2===0) total += i | |
| } | |
| console.log(total) |
This file contains hidden or 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
| /* Predict: | |
| 0+1=1 1+1=2. | |
| the growth of vine is the previous two added, n-1+n-2, which is basically fibonacci | |
| */ | |
| // A vine's new growth each week equals the total of the previous | |
| // two weeks' growth. Week 0 grew 0 cm, week 1 grew 1 cm. | |
| let lastWeekGrowth: number = 0; | |
| let thisWeekGrowth: number = 1; | |
| for (let week = 1; week <= 5; week++) { |
This file contains hidden or 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
| /* Predict: | |
| 40, 20, 10, 5, 2, 1 | |
| 0, 1, 2, 3, 4, 5 | |
| */ | |
| // A knockout tournament: each round, half the players are eliminated | |
| // (odd player out sits the round). How many rounds until a winner? | |
| let playersLeft: number = 1000; | |
| let roundsPlayed: number = 0; | |
| while (playersLeft > 1) { |
NewerOlder