Simple Function in TypeScript
- A function name starts with a letter or underscore, followed by any number of letters, numbers, or underscores.
- Function names are case-insensitive.
- A function is a block of code that can be executed whenever we need it.
- The name of a function cannot be a global identifier.
- The name of a function cannot be a reserved keyword of the TypeScript language.
Step 1
Step 2
Coding
simple_function.ts
- class simplefunction {
- armstrong() {
- var min: number, max: number;
- var num: number, r: number, sum: number, temp: number;
- min = parseInt(prompt("Enter a minimum range"));
- max = parseInt(prompt("Enter a maximum range"));
- for (num = min; num <= max; num++) {
- temp = num;
- sum = 0;
- while (temp != 0) {
- r = Math.floor(temp % 10);
- sum = sum + (r * r * r);
- temp = Math.floor(temp / 10);
- }
- if (sum == num)
- alert("Armstrong number is ->" + num);
- }
- }
- }
- window.onload = () => {
- var greeter = new simplefunction();
- greeter.armstrong();
- };
demo.html
- < !DOCTYPEhtml >
- <
- htmllang = "en"
- xmlns = "http://www.w3.org/1999/xhtml" >
- <
- head >
- <
- metacharset = "utf-8" / >
- <
- title > Simple Function < /title>
- <
- linkrel = "stylesheet"
- href = "app.css"
- type = "text/css" / >
- <
- scriptsrc = "app.js" > < /script>
- <
- /head>
- <
- body >
- <
- h2 > Simple Function in TypeScript HTML App < /h2>
- <
- divid = "content" / >
- <
- /body>
- <
- /html>
app.js
- var simplefunction = (function() {
- function simplefunction() {}
- simplefunction.prototype.armstrong = function() {
- var min;
- var max;
- var num;
- var r;
- var sum;
- var temp;
- min = parseInt(prompt("Enter a minimum range"));
- max = parseInt(prompt("Enter a maximum range"));
- for (num = min; num <= max; num++) {
- temp = num;
- sum = 0;
- while (temp != 0) {
- r = Math.floor(temp % 10);
- sum = sum + (r * r * r);
- temp = Math.floor(temp / 10);
- }
- if (sum == num) {
- alert("Armstrong number is ->" + num);
- }
- }
- };
- return simplefunction;
- })();
- window.onload = function() {
- var el = document.getElementById('content');
- var greeter = new simplefunction();
- greeter.armstrong();
- };





Join the conversation! Your thoughts help the community grow.