{{ message }}

Instantly share code, notes, and snippets.

# Joyce-O/index.html

Last active Dec 7, 2019
Narcissistic Numbers


Hey!

Do you know that humans are not the only ones that have to put up with narcissists?

|

Enter a number below to see if your favorite number is in fact a narcissistic number.

## Narcissistic Numbers

##Programme that takes a positive integer and checks if it is a narcissistic number or not. For example: 153 (3 digits): 1^3 + 5^3 + 3^3 = 1 + 125 + 27 = 153 1634 (4 digits): 1^4 + 6^4 + 3^4 + 4^4 = 1 + 1296 + 81 + 256 = 1634

A Pen by joyce obi on CodePen.

 const submit = document.getElementById('submit'); const enter = document.getElementById('enter'); const narc = document.getElementById('narc'); const narcno = document.getElementById('narcno'); const leg = document.getElementById('leg'); const modal = document.getElementById('modal'); const yes = document.getElementById('yes'); const no = document.getElementById('no'); const txt = document.getElementById("txt"); const textarea = document.getElementById("textarea"); const searchForm = document.getElementById("search-form"); const isNarcissistic = (num) => { let digits = Array.from(String(num), Number); const sum = digits.reduce((acc, curr) => { return acc + Math.pow(Number(curr), digits.length); }, 0); return (sum === num); }; const display = () => { let num = document.getElementById("num").value; if(num === "") { console.log("Number expected"); return; } num = Number(num); searchForm.style.display = "none"; enter.style.display = "none"; if (isNarcissistic(num)=== true) { // alert("YES"); textarea.style.borderColor = "green"; leg.style.display = "block"; let displayNum = narc.innerHTML = num; narc.style.display = "block"; txt.innerHTML = `Yes! \${num} is a narcissist!!`; txt.style.display = "block"; modal.style.display = "flex"; modal.style.justifyContent = "center"; } else if (isNarcissistic(num) === false) { textarea.style.borderColor = "blue"; let displayNum = narcno.innerHTML = num; narcno.style.display = "block"; txt.innerHTML = `Nope! \${num} is NOT a narcissist!!`; txt.style.display = "block"; modal.style.display = "flex"; modal.style.justifyContent = "center"; } else { return; } } //console.log(isNarcissistic(153)); const yesModal = () => { modal.style.display = 'none'; txt.style.display = "none"; leg.style.display = "none"; narcno.style.display = "none"; narc.style.display = "none"; if (yes) { searchForm.style.display = "block"; textarea.style.borderColor = "rgb(32, 17, 19)"; enter.style.display = "inline-block"; } return; } // const noModal = () => { // modal.style.display = 'none'; // } submit.addEventListener('click', display); yes.addEventListener('click', yesModal); // no.addEventListener('click', noModal);