Write a program that computes the value of a+aa+aaa+aaaa with a given digit as the value of a in JavaScript.
Write a JavaScript program that accepts an integer (n) and computes the value of n+nn+nnn.
JavaScript Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 | <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <meta http-equiv="X-UA-Compatible" content="ie=edge"> <title> Title </title> </head> <body> <input type="text" id="digit" placeholder="Enter Digits" > <input type="text" id="number" placeholder="Enter a Number"> <button id="btn">Calculate</button> <div id="result"></div> <script> const digit= document.querySelector("#digit"); const number= document.querySelector("#number"); const btn= document.querySelector("#btn"); const result= document.querySelector("#result"); btn.onclick=function(){ let digitVal = Number(digit.value); let numVal = number.value; let sum = 0; for(let i=0;i<=digitVal;i++){ let nbr = ""; for(let j=0;j<i;j++){ nbr+=numVal; } result.innerHTML+=nbr+"<br>"; sum+=Number(nbr); } result.innerHTML+="______________+<br>"; result.innerHTML+=sum+"<br>"; } </script> </body> </html> |
Output: