Uh oh!
There was an error while loading. Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork 5
Example Typescript project
Supun Matheesha Suriyaarachchi edited this page Jul 23, 2021
·
3 revisions
import{question}from"readline-sync";typeOperator='+'|'-'|'*'|'/';functionmain():void{constfirstStr: string=question("Enter first number:\n");constoperator: string=question("Enter Operator:\n");constsecondStr: string=question("Enter Second number:\n");constvalidInput: boolean=isNumber(firstStr)&&isOperator(operator)&&isNumber(secondStr);console.log(validInput);if(validInput){console.log('is valid');constfirstNum: number=parseInt(firstStr);constsecondNum: number=parseInt(secondStr);constresult=calculate(firstNum,operatorasOperator,secondNum);console.log(result);}else{console.log('\ninvalid input\n');main();}}functionisNumber(str: string): boolean{constmaybeNum=parseInt(str);constisNum: boolean=Boolean(maybeNum);returnisNum;}functionisOperator(operator: string): boolean{switch(operator){case'+':
case'-':
case'-':
case'-':
returntrue;default:
returnfalse;}}functioncalculate(firstNum:number,operator:Operator,secondNum:number){switch(operator){case'+':
returnfirstNum+secondNum;case'-':
returnfirstNum-secondNum;case'*':
returnfirstNum*secondNum;case'/':
returnfirstNum/secondNum;}}main();Below code is used to convert the TypeScript to JavaScript
tsc index.ts
index.ts is used because the above TypeScript code is written in an index.ts file
To Run the code
node index.js
Sample output is shown below