Build a Spy Number Checker using HTML CSS and JavaScript (original) (raw)

Last Updated : 25 Jul, 2024

In the realm of mathematics, Spy Numbers, also known as secretive numbers or cryptic numbers, possess a unique property. A spy number is defined as a number whose sum of digits is equal to the product of its digits. In this article, we will explore how to build a Spy Number Checker using HTML, CSS, and JavaScript.

**Example 1: Identifying a Spy Number

Let's take the number 112 as an example and run it through our Spy Number Checker.

**Explanation:

Sum of Digits: 1 + 1 + 2 = 4
Product of Digits: 1 * 1 * 2 = 2

In this case, the sum of the digits (4) is not equal to the product of the digits (2), indicating that 112 is not a spy number.

**Example 2:

Validating a Spy Number Now, let's consider the number 22 and check if it qualifies as a spy number.

**Explanation:

Sum of Digits: 2 + 2 = 4
Product of Digits: 2 * 2 = 4

In this instance, the sum of the digits (4) matches the product of the digits (4), confirming that 22 is indeed a spy number.

**Prerequisites

**Approach

**Example: This example shows the implementation of the above-explained approach.

HTML `

Spy Number Checker

Spy Number Checker

Check

<script src="script.js"></script>

CSS

/* style.css */ .container { text-align: center; margin-top: 50px; }

input { padding: 5px; }

button { padding: 8px 16px; margin-top: 10px; }

#result { margin-top: 20px; font-weight: bold; font-size: 18px; }

` JavaScript ``

// script.js

function checkSpyNumber() { const numberInput = document.getElementById("numberInput").value;

const digits = 
    numberInput.toString().split("").map(Number);
const sum = 
    digits.reduce((a, b) => a + b, 0);
const product = 
    digits.reduce((a, b) => a * b, 1);

if (sum === product) {
    document.getElementById(
        "result"
    ).textContent = `${numberInput} is a Spy Number!`;
} else {
    document.getElementById(
        "result"
    ).textContent = `${numberInput} is not a Spy Number.`;
}

}

``

**Output

Spy-Number-Checker-using-HTML-CSS-and-JavaScript