In: Computer Science
<!DOCTYPE html>
<html>
<head>
<!-- *************************************************************************************** -->
<!-- * * -->
<!-- * Do not change anything within the <head></head> section of the HTML * -->
<!-- * * -->
<!-- *************************************************************************************** -->
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.1.3/css/bootstrap.min.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.3/umd/popper.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.1.3/js/bootstrap.min.js"></script>
<style> .btn{border:1px solid black; padding:5px;display:inline-block} </style>
</head>
<body>
<!-- *************************************************************************************** -->
<!-- * * -->
<!-- * Nothing to change here in the <body> * -->
<!-- * * -->
<!-- *************************************************************************************** -->
<h1>Count of Magic numbers</h1>
<p>Enter a number x. Click calculate to determine the number of magic numbers between 1 and x (inclusive).</p>
<p>Your number: <input type="text" id="textEntered1"></p>
<p><input type="button" class="btn btn-primary" id="btn_1" value="Calculate"></p>
<div id="textDisplayed1"></div>
</body>
<script>
//
function isMagicNumber(x) {
////////////////////////////////////////////////////////////////////////////////
// Insert your code between here and the next comment block. Do not alter //
// any code in any other part of this file. //
////////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////////
// Insert your code between here and the previous comment block. Do not alter //
// any code in any other part of this file. //
/////////////////////////////////////////////////////////////////////////////////
}
function getCount(x) {
////////////////////////////////////////////////////////////////////////////////
// Insert your code between here and the next comment block. Do not alter //
// any code in any other part of this file. //
////////////////////////////////////////////////////////////////////////////////
var numbers = "";
for(var i = 1; i<x; i++)
{
if(isMagicNumber(i))
{
numbers += "" + i + ",";
}
}
return numbers;
/////////////////////////////////////////////////////////////////////////////////
// Insert your code between here and the previous comment block. Do not alter //
// any code in any other part of this file. //
/////////////////////////////////////////////////////////////////////////////////
}
$("#btn_1").click(function(){
var x = $('#textEntered1').val();
$("#textDisplayed1").html(getCount(x));
});
</script>
</html>
Code changes and relevant comments have been added in bold.
<!DOCTYPE html>
<html>
<head>
<!-- *************************************************************************************** -->
<!-- * * -->
<!-- * Do not change anything within the <head></head> section of the HTML * -->
<!-- * * -->
<!-- *************************************************************************************** -->
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.1.3/css/bootstrap.min.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.3/umd/popper.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.1.3/js/bootstrap.min.js"></script>
<style> .btn{border:1px solid black; padding:5px;display:inline-block} </style>
</head>
<body>
<!-- *************************************************************************************** -->
<!-- * * -->
<!-- * Nothing to change here in the <body> * -->
<!-- * * -->
<!-- *************************************************************************************** -->
<h1>Count of Magic numbers</h1>
<p>Enter a number x. Click calculate to determine the number of magic numbers between 1 and x (inclusive).</p>
<p>Your number: <input type="text" id="textEntered1"></p>
<p><input type="button" class="btn btn-primary" id="btn_1" value="Calculate"></p>
<div id="textDisplayed1"></div>
</body>
<script>
//
function isMagicNumber(x) {
var sum = 0;
//magic number is when a digits of a number are added till the
point, sum becomes a single digit number and sum is equal to
1
while (x > 0 || sum > 9)
{
if (x == 0)
{
x = sum;
sum = 0;
}
sum += x % 10;
x /= 10;
}
// returning true if sum is equal 1.
return (sum == 1);
}
function getCount(x) {
var numbers = "";
for(var i = 1; i<x; i++)
{
if(isMagicNumber(i)) //Checking if number is magic or not
{
numbers += "" + i + ","; // The magic number will be added one by one separated by comma(',')
}
}
return numbers; // The number will be returned as 10,19,28, if the input is 30 for instance.
}
$("#btn_1").click(function(){
var x = $('#textEntered1').val();
$("#textDisplayed1").html(getCount(x));
});
</script>
</html>