do while Loop in Typescript

TypeScript do while loop

The do-while loop is similar to the while loop except that the conditional expression is tested at the end of the loop. The do-while statement executes the block of statements within its braces as long as its conditional expression is true. When you use a do-while statement, the condition is tested at the end of the do-while loop. this means the code will always be executed at least once.
 
Syntax
  1. do {}  
  2. while (initialize counter < condition);   
The following example shows the palindrome of a given number. A palindrome number is a number that remains the same when its digits are reversed. In this example, I have a dowhile class and define a dowhile loop. Let's see how I implement the while loop in TypeScript. Let's use the following.
 
Step 1
 
Open Visual Studio 2012 and click "File" -> "New" -> "Project...". A window is opened. In this window, click HTML Application for TypeScript under Visual C#. Give the name of your application as "palindrome" and then click ok.
 
Step 2
 
After this session, the project has been created; a new window is opened on the right side. This window is called the Solution Explorer. The Solution Explorer contains the ts file, js file, css file, and html file.
 
Coding
 
palindrome.ts
  1. class dowhile {  
  2.  constructor() {}  
  3.  palimdrome() {  
  4.   var n: number;  
  5.   var s = 0;  
  6.   n = parseInt(prompt("Enter a Number for Palindrome or Not"));  
  7.   var temp = n;  
  8.   do {  
  9.    s = s * 10;  
  10.    s = s + temp % 10;  
  11.    temp = Math.floor(temp / 10);  
  12.   }  
  13.   while (temp != 0);  
  14.   if (s === n) {  
  15.    var span = document.createElement("span");  
  16.    span.innerText = "Number is palindrome->" + n;  
  17.    document.body.appendChild(span);  
  18.   } else {  
  19.    var span = document.createElement("span");  
  20.    span.innerText = "Number is not palindrome->" + n;  
  21.    document.body.appendChild(span);  
  22.   }  
  23.  }  
  24. }  
  25. window.onload = () => {  
  26.  var greeter = new dowhile();  
  27.  greeter.palimdrome();  
  28. };  
palindromexample.html
  1. < !DOCTYPE html >  
  2.  <  
  3.  html lang = "en"  
  4. xmlns = "http://www.w3.org/1999/xhtml" >  
  5.  <  
  6.  head >  
  7.  <  
  8.  meta charset = "utf-8" / >  
  9.  <  
  10.  title > Palindrome example < /title>  
  11.  <  
  12.  link rel = "stylesheet"  
  13. href = "app.css"  
  14. type = "text/css" / >  
  15.  <  
  16.  script src = "app.js" > < /script>  
  17.  <  
  18.  /head>  
  19.  <  
  20.  body >  
  21.  <  
  22.  h1 > Palindrome Example in TypeScript HTML App < /h1>  
  23.  <  
  24.  div id = "content" / >  
  25.  <  
  26.  /body>  
  27.  <  
  28.  /html>  
app.js
  1. var dowhile = (function() {  
  2.  function dowhile() {}  
  3.  dowhile.prototype.palimdrome = function() {  
  4.   var n;  
  5.   var s = 0;  
  6.   n = parseInt(prompt("Enter a Number for Palindrome or Not"));  
  7.   var temp = n;  
  8.   do {  
  9.    s = s * 10;  
  10.    s = s + temp % 10;  
  11.    temp = Math.floor(temp / 10);  
  12.   } while (temp != 0)  
  13.   if (s === n) {  
  14.    var span = document.createElement("span");  
  15.    span.innerText = "Number is palindrome->" + n;  
  16.    document.body.appendChild(span);  
  17.   } else {  
  18.    var span = document.createElement("span");  
  19.    span.innerText = "Number is not palindrome->" + n;  
  20.    document.body.appendChild(span);  
  21.   }  
  22.  };  
  23.  return dowhile;  
  24. })();  
  25. window.onload = function() {  
  26.  var greeter = new dowhile();  
  27.  greeter.palimdrome();  
  28. };  
Output 1
 
palindrom-number-type-script.gif
 
Click the "Ok" button.
 
Output 2
 
final-result-palindrom-type-script.gif
 
Reference By
 
http://www.typescriptlang.org/


Similar Articles