[Leaning Typescript ๐ฆ] system of a clown
๐คก system of a clown
๐ Help me, please!
I planned an elaborate clown-themed surprise party for my best friend. The whole event was planned using a few files of TypeScript code Iโd written.
My friend somehow caught wind of the party and enlisted the clownsโ help to vandalize the program. Not only did the clowns remove or make incorrect my TypeScript type annotations, they introduced subtle bugs by subtly changing some of the values.
Could you please add correct type annotations in my files and fix any bugs detected by TypeScript? Youโre my surprise clown celebrationโs last hope! ๐คก
๐ ๋์์ฃผ๋ผ์ฃผ!
๋ด ๋ฒ ํ๋ฅผ ์ํด ์์๋ก ์ปจ์
์ ๊น์ง ํํฐ๋ฅผ ์ค๋นํ์ด.
๋ชจ๋ ์ด๋ฒคํธ๊ฐ ๋ด๊ฐ ์ด ๋ช๊ฐ์ ํ์
์คํฌ๋ฆฝํธ ํ์ผ๋ก ์ค๋น๋์ด ์์ง.
๊ทผ๋ฐ ๋ด ์น๊ตฌ๊ฐ ์ด๋ป๊ฒ๋ ์ ๋ณด๋ฅผ ๋ค์ด๊ฐ์ง๊ณ ํ๋ก๊ทธ๋จ์ ํ๊ดดํ๊ธฐ ์ํด ์์๋ก๋คํํ
๋์์ ์์ฒญํ๊ฑฐ์ผ!
์์๋ก๋ค์ ๋ด ํ์
์คํฌ๋ฆฝํธ ์ ๋ํ
์ด์
์ ์ง์ฐ๊ณ ๋ง๊ฐ๋จ๋ฆฌ๋๊ฒ ๋ฟ๋ง ์๋๋ผ, ์ผ๋ถ ๊ฐ๋ค์ ๊ต๋ฌํ๊ฒ ๋ฐ๊ฟ๋ฒ๋ฆฌ๋ฉฐ ๋ฒ๊ทธ๋ง์ ๋์
ํด๋ฒ๋ ธ์ด.
๋ด ํ์ผ์ ์ฌ๋ฐ๋ฅธ ์ ๋ํ
์ด์
์ ์ถ๊ฐํ๊ณ ๋ฒ๊ทธ๋ฅผ ์ก์์ค ์ ์์ด? ๋๋ ๋ด ๊น์ง ์์๋ก ํํฐ์ ๋ง์ง๋ง ํฌ๋ง์ด์ผ! ๐คก
ํ์ ์ ๋ํ ์ด์ (type annotation)? ํ์ ์ ์ฃผ์์ ๋ค๋ ๊ฒ์ผ๋ก ๋ณ์๋ ํจ์์ ํ์ ์ ์ค์ ํ๋ ๊ฒ.
ex) let name: string = โsujinโ
๐ Step 1: Clowning Around
The first area of code Iโll need you to fix is the activity assignment engine. It repeatedly creates groups of size 5-10 guests. Each group is directed to go to an activity, which can hold up to a certain number of them at a time.
It looks like the clowns didnโt change too much on this one. They mostly just removed a few type annotations and changed one value.
Could you please add back missing type annotations to avoid evolving or implicit anys, and fix the wrong value?
โช ์์ฝ: ์กํฐ๋นํฐ ํ ๋น ์์ง์์ ํ์ ์ ๋ํ ์ด์ ์ ์ถ๊ฐํ๊ณ ์ค๋ฅ๋ ๋ถ๋ถ์ ๊ณ ์ณ๋ผ!
๐ ๋ฌธ์ ์ฝ๋
let remainingGuests = 20;
while (true) {
// Each group of guests will be size 5-10
const guestsToAssign = Math.floor(Math.random() * 5) + 5;
let activity; // Those were some nice type annotations you had here!
let capacity; // It'd be a shame if we ... *erased* them! ๐
let requiresSupplies;
switch (Math.floor(Math.random() * 5)) {
case 0:
activity = "balloon animals";
capacity = 5;
requiresSupplies = true;
break;
case 1:
activity = "face painting";
capacity = "1";
requiresSupplies = true;
break;
case 2:
activity = "juggling";
capacity = 3;
requiresSupplies = true;
break;
default:
activity = "dancing";
capacity = 10;
break;
}
console.log(`${guestsToAssign} of us will enjoy ${activity}.`);
for (let i = 0; i < guestsToAssign; i += capacity) {
console.log(`\t${capacity} will enter the ${activity} activity.`);
}
remainingGuests -= guestsToAssign;
if (remainingGuests < 0) {
break;
}
console.log(`We have ${remainingGuests} remaining guests to entertain.\n`);
}
console.log("\nAll done!");
export {};
๋ฌธ์ ๋ก ๋์จ ์ด ์ฝ๋๋ฅผ tsc๋ช ๋ น์ด๋ฅผ ์ด์ฉํด ์ปดํ์ผํ๋ฉด ๋ค์๊ณผ ๊ฐ์ ์ค๋ฅ๊ฐ ๋ฐ์ํฉ๋๋ค.
์ฐ์ ์ฐ์ฐ์์ string
๊ฐ์ด ํ ๋น๋๊ณ ์๋ ์ํฉ์
๋๋ค.
๐ ํ์ด ๊ณผ์
์ค๋ฅ๊ฐ ๋ ๋ถ๋ถ์ ๋ณด๋, ์ฐ์ ์ฐ์ฐ์์ string
๊ฐ์ด ํ ๋น๋์ด ์ปดํ์ผ๊ณผ์ ์์ ๋งํ๋ฒ๋ ธ์ต๋๋ค.
ํด๋น ๋ณ์ capacity
๋ฅผ ์ฐพ์ต๋๋ค.
switch๋ฌธ์์์ ๋ฌธ์์ด๋ก ๋์ด ์๋ capacity
๋ฅผ ์ฐพ์ ์ซ์๋ก ๊ณ ์นฉ๋๋ค.
case 1:
activity = "face painting";
// capacity = "1";
capacity = 1; // ์์
requiresSupplies = true;
break;
์๊ตฌ์ฌํญ๋๋ก ๋ณ์๊ฐ ์ ์ธ๋ ๋ถ๋ถ์์ ํ์ ์ ๋ํ ์ด์ ๋ ์ถ๊ฐํฉ๋๋ค.
let activity; // Those were some nice type annotations you had here!
let capacity; // It'd be a shame if we ... *erased* them! ๐
let requiresSupplies;
โ ์์ ๋ ์ ์ฒด ์ฝ๋
let remainingGuests = 20;
while (true) {
// Each group of guests will be size 5-10
const guestsToAssign = Math.floor(Math.random() * 5) + 5;
let activity: string; // Those were some nice type annotations you had here!
let capacity: number; // It'd be a shame if we ... *erased* them! ๐
let requiresSupplies: boolean;
switch (Math.floor(Math.random() * 5)) {
case 0:
activity = "balloon animals";
capacity = 5;
requiresSupplies = true;
break;
case 1:
activity = "face painting";
capacity = 1;
requiresSupplies = true;
break;
case 2:
activity = "juggling";
capacity = 3;
requiresSupplies = true;
break;
default:
activity = "dancing";
capacity = 10;
break;
}
console.log(`${guestsToAssign} of us will enjoy ${activity}.`);
for (let i = 0; i < guestsToAssign; i += capacity) {
console.log(`\t${capacity} will enter the ${activity} activity.`);
}
remainingGuests -= guestsToAssign;
if (remainingGuests < 0) {
break;
}
console.log(`We have ${remainingGuests} remaining guests to entertain.\n`);
}
console.log("\nAll done!");
export {};
๐ Step 2: Clown Availability
The second and -thank heavens- final area of code Iโll need you to fix is my clown assignment planner. I use this to print which clowns will be partying with which guests.
Oh, and they introduced several unsettling clown puns in comments.
Itโs because of those puns that I canโt fix the code myself.
The clowns again changed a few things around, but nothing more than an incorrect type annotation or different value for a variable.
ยซยซยซ< HEAD Could you please fix up the file to have no TypeScript complaints? x ======= Could you please fix up the file to have no TypeScript complaints?
โช ์์ฝ: ๊ณ ๋ง์ด๋ฐ ์ผํ๋ ๋ ํด๋ผ! ๊ด๋ ๋ฐฐ์ ํ๋๋์์ ์ค๋ฅ๋ฅผ ์์ ํด๋ผ!
๐ ๋ฌธ์ ์ฝ๋
// Note: I'm planning on inviting 20 guests in total.
// Some clowns can only handle a certain number of guests.
let guestCount: boolean = 20;
let clownsCount = "zilch!";
let krustyAvailability = true;
let ronaldAvailability = true;
let pennywiseAvailability = true;
let matchingsDescription: any = "";
let lastClown;
do {
clownsCount += 1;
// Krusty says: I had a one-man show on Broadway...
// That's who showed up, one man!
if (krustyAvailability) {
guestCount -= 10;
krustyAvailability = false;
matchingsDescription += "Krusty will handle the first ten guests.\n";
lastClown = "Krusty";
continue;
}
// Ronald says: McDonald's donated a large set of computers to a school...
// They were all Big Macs!
if (ronaldAvailability) {
guestCount -= 5;
ronaldAvailability = false;
matchingsDescription += "Ronald will handle the next five guests.\n";
lastClown = "Ronald";
continue;
}
// Pennywise asks: what's a sewer's favorite data type?
// Pennywise answers: Floats!
if (pennywiseAvailability) {
pennywiseAvailability = false;
matchingsDescription += "Pennywise wฬบฬฬ iฬขออlอฬlออฬ cออฬoฬ ฬฬกnฬผฬsฬอฬกuอฬอmอฬชฬeฬขอฬ yฬดอฬฟอฬกอฬกoฬธอฬฬพอฬขอuฬธอออฬกฬกrฬธออออออ sฬตออฬฬบฬฬชoฬตฬฬฝอฬกอฬuฬตอออฬ อlฬธฬฬฬอฬอsฬธฬฝออฬบอ.";
lastClown = "Pennywise";
continue;
}
throw new Error(`Oh no! We're out of clowns!`);
} while (guestCount > 0);
if (clownsCount > 2) {
console.log("We've got a lot of clowns coming!");
}
if (matchingsDescription.length()) {
console.log(`There will be ${clownsCount} clowns!\n`);
clownsole.log(matchingsDescription);
console.log(`The last clown is: ${lastClown.toUpperCase()}!`);
} else {
console.log("Nobody gets a clown. Terrible party. Goodbye.");
}
export {};
1๋ฒ ๋ฌธ์ ๋ณด๋ค ๋ง์ ์ค๋ฅ๊ฐ ๋ฐ์ํ์ต๋๋ค.
๐ ํ์ด๊ณผ์
โช Type 'number' is not assignable to type 'boolean'.
guestCount
์ ํ์
์ด number์ธ๋ฐ boolean์ผ๋ก ํ์
์ ๋ํ
์ด์
์ค์ ์ด ์๋ชป๋์ด ์์ต๋๋ค.
// ๋ฌธ์ ์ฝ๋
let guestCount: boolean = 20;
// ์์ ๋ ์ฝ๋
let guestCount: number = 20;
โช Operator '>' cannot be applied types 'string' and 'number'.
clownCount
์ ์ฐ์ ์ฐ์ฐ์๋ฅผ ์ฌ์ฉํ๊ณ ์๋๋ฐ ์ ์ธ๋ถ์ ๋ณด๋ฉด ๋ฌธ์์ด์ ํ ๋นํ๊ณ ์์ต๋๋ค.
zilch
๊ฐ ๋ฌด์จ ๋ป์ธ์ง ์ฐพ์๋ณด๋ ์๋ฌด๊ฒ๋ ์์
์ด๋ผ๋ ๋ป์ด๋ผ ๊ฐ์ ํ ๋นํ์ง ์์์ต๋๋ค.
let
์ผ๋ก ์ ์ธ๋์๊ธฐ ๋๋ฌธ์ ๊ฐ๋ฅํฉ๋๋ค.
// ๋ฌธ์ ์ฝ๋
let clownsCount = "zilch!";
...
if (clownsCount > 2) {
console.log("We've got a lot of clowns coming!");
}
// ์์ ๋ ์ฝ๋
let clownCount;
์๋ก์ด ์๋ฌ๋ฅผ ๋ง๋ค๋ฉฐ ํด๊ฒฐ๋์ง ์์์ต๋๋ค.
๊ฐ์ด ํ ๋น๋๊ธฐ ์ ์ ์ฐ์ ์ฐ์ฐ์๋ฅผ ์ ์ฉํ ์ ์๊ธฐ ๋๋ฌธ์ ๋๋ค.
์ด๊ธฐ๊ฐ์ผ๋ก 0
์ ํ ๋นํด์ฃผ์์ง๋ง, ๋ญ๊ฐ ๋ต์ด ์๋๊ฒ๋ง ๊ฐ์ ๋๋์ด ๋๋ค์.
// ์์ ๋ ์ฝ๋
let clownCount: number = 0;
โช Cannot find name 'clownsole'. Did you mean 'console'?
clownsole
์ ์ฒ์ ๋ค์ด๋ด
๋๋ค. ๋ฌธ์ ๊ฐ ์ ๋ฅผ ๋ฐ๋ณด๋ก ์๋๋ด
๋๋ค. ์ฌ๋ฐ๋ค์.
// ๋ฌธ์ ์ฝ๋
clownsole.log(matchingsDescription);
// ์์ ๋ ์ฝ๋
let clownCount: number = 0;
console.log(matchingsDescription);
๋!
Found 0 errors. Watching for file changes.
๐ ๊ณ ์ณ์ผ ํ ์
let guestCount = 20;
let clownsCount = 0;
์๋ฃจ์ ์ฝ๋๋ฅผ ๋ณด๋ ํ์ ์ ๋ํ ์ด์ ์ด ๋น ์ ธ์์ต๋๋ค.
์ฑ ์ ์ฝ์ ๋ ๊ฐ์ ํ ๋นํ ๊ฒฝ์ฐ ์๋์ผ๋ก ํ์ ์ถ๋ก ์ด ๋๊ธฐ ๋๋ฌธ์ ํ์ ์ ๋ํ ์ด์ ์ ์ค์ ํ๋ฉด ์ค๋ณต์ผ๋ก ํ์ ์ค์ ๊ณผ์ ์ด ์งํ๋๋ ๊ฒ๊ณผ ๋ง์ฐฌ๊ฐ์ง๋ผ ๋ถํ์ํ ๊ณผ์ ์ด๋ผ๊ณ ํ ๊ธฐ์ต์ด ๋ฉ๋๋ค.
โ ๊ฐ์ ํ ๋นํด ํ์ ์ถ๋ก ์ด ๋ ๊ฒฝ์ฐ์๋, ํ์ ์ ๋ํ ์ด์ ์ ์๋ตํ์
๊ทธ๋๋ 0
์ ํ ๋นํ๋๊ฒ ๋ง์๋ค์ ๐
Leave a comment