Skip to content
This repository was archived by the owner on Oct 12, 2022. It is now read-only.

Commit f0db5e7

Browse files
committed
Create simple class which requires an existing js module.
1 parent 4148917 commit f0db5e7

File tree

1 file changed

+40
-0
lines changed

1 file changed

+40
-0
lines changed

js-and-ts/robot.ts

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
// This import wouldn't be possible without the allowJS option in tsconfig
2+
import { Formatter } from './format.js';
3+
4+
interface Robot {
5+
name: String,
6+
currentComputation: Number
7+
}
8+
9+
class Robot {
10+
constructor(public name: String) {
11+
this.name = name;
12+
this.currentComputation = 0;
13+
}
14+
15+
// Given a mathematical operation, return a value based on the value passed,
16+
// the operation and the number 10
17+
compute(operation, value) {
18+
let computedValue = 0;
19+
switch(operation) {
20+
case '+':
21+
computedValue = value + 10
22+
case '-':
23+
computedValue = value - 10
24+
case '/':
25+
computedValue = value / 10
26+
case '*':
27+
computedValue = value * 10
28+
}
29+
this.currentComputation = computedValue;
30+
}
31+
32+
// Using an external JS module, format the computed value from our robot
33+
displayCurrentComputation() {
34+
console.log(Formatter.surroundWithStars(this.currentComputation));
35+
}
36+
}
37+
38+
const hal = new Robot('Hal');
39+
hal.compute('+', 32);
40+
hal.displayCurrentComputation();

0 commit comments

Comments
 (0)