Stats: 3,228,656 members, 8,079,800 topics. Date: Sunday, 16 February 2025 at 10:59 PM |
Nairaland Forum / Science/Technology / Programming / Javascript,html Help (1115 Views)
Tarot Website I Cooked With JavaScript,HTML / Quadratic Equations Calculator Made With Javascript, HTML And CSS / After 10days Of Coding With Html, Css And Javascript Forum4africa Is Ready (2) (3) (4)
Javascript,html Help by kaykay100(f): 11:25pm On Mar 03, 2019 |
I am new to coding and I am working on this exercise. I am writing program to check whether an integer is Armstrong number or not. Example: 371 is an Armstrong number since 3 * * 3 + 7 * *3 + 1 * * 3 = 371. Almost done but need to be able to have the following. 1- a div to display the result. If the number is not a 3 digit integer, the button should be deactivated and if the user tries to click on it while deactivated, a message should appear explaining why the button can’t be clicked. 2. A div to display the result So far I have been a screen alert to make sure I see my function is working fine. Which it is but need to sort out these 2 mentioned above. Here is my code; javascript function Armstrong() { var flag,number,remainder,addition = 0; number = Number(document.getElementById("inputnum" ![]() btn = document.getElementById("checkbtn" ![]() btn.disabled = true; number.onkeyup = () => { if ( number.length === 3) { btn.disabled = false; } else { btn.disabled = true; } } flag = number; while(number > 0) { remainder = number%10; addition = addition + remainder*remainder*remainder; number = parseInt(number/10); } if(addition == flag) { window.alert("-The inputed number is Armstrong" ![]() } else { window.alert("-The inputed number is not Armstrong" ![]() } } -- HTML--- <html> <body> <center><h1>The Armstrong Detector App</h1> Enter your value: <input id="inputnum" type="text" class="input" > <center><button onclick="Armstrong()" class="button" id="checkbtn" >Check</button> <br /> </body> </html> |
Re: Javascript,html Help by stanliwise(m): 2:36am On Mar 04, 2019 |
Note: Quote me to properly copy my code if possible Your code have many errors and I don't have the time for review I want to sleep. 1. You called an event inside a function ![]() 2. You tried using number the event number.onkeyup() which would never work because number is not an object since you have already assign number with document.getElementById(...).value instead of simply document.getElementById(...) 2. Armstrong function is not responsible for collecting data from div elements, all it needed was just an input 3. Always use inline event handler rather than the traditional html event hanldler <html onclick = function(...) > 4. <center> tag is deprecated. 5. Your html code doesn't begin with <!Doctype html> 6.You didn't document complicated part of your code, 1 Like |
Re: Javascript,html Help by stanliwise(m): 2:59am On Mar 04, 2019 |
kaykay100:Above is the solution. 1 Like |
Re: Javascript,html Help by stanliwise(m): 8:53pm On Mar 04, 2019 |
kaykay100:Did it work?? 1 Like |
Re: Javascript,html Help by kaykay100(f): 8:55pm On Mar 05, 2019 |
Thank you much for breaking it down more. Nairaland did not give ma a notification that someone answered my question ,I am truely sorry for the late reply. I ran the code but when i click the button it does not work BECAUSE the function has not been called anywhere in the HTML code. Below is the code; <--- HTML---> <html> <body> <center> <h1>The Armstrong Detector App</h1> Enter your value: <br/> <input id="inputnum" type="text" class="input"> <button class="button" id="checkbtn" >Check</button> </center> </body> </html> <---- JAVASCRIPT---> function Armstrong(number){ var flag,remainder,addition = 0; flag = number; while( number > 0 ){ remainder = number % 10; addition += remainder * remainder * remainder; number = parseInt( number/10 ); } if(addition == flag){ window.alert("-The inputed number is Armstrong" ![]() }else { window.alert("-The inputed number is not Armstrong" ![]() } } |
Re: Javascript,html Help by kaykay100(f): 9:08pm On Mar 05, 2019 |
stanliwise: Yes it did work ,thank you so much . Here is the code. But the only thing that is not working is ,when the user clicks the "disabled" button. There should a message telling them they should enter 3 digits. How do I do that? var number, btn; number = document.getElementById("inputnum" ![]() btn = document.getElementById("checkbtn" ![]() btn.disabled = true; //events number.onkeyup = () => { if (number.value.length === 3) { btn.disabled = false; } else { btn.disabled = true; } } //inline method of onclick event handler for check button btn.onclick = () =>{ Armstrong(number.value); } function Armstrong(number){ var flag,remainder,addition = 0; flag = number; while( number > 0 ){ remainder = number % 10; addition += remainder * remainder * remainder; number = parseInt( number/10 ); } if(addition == flag){ window.alert("-The inputed number is Armstrong" ![]() }else { window.alert("-The inputed number is not Armstrong" ![]() } } |
Re: Javascript,html Help by stanliwise(m): 11:48pm On Mar 05, 2019 |
kaykay100:figured out that your self. the onclick event function should check the value length of the input and make the appropriate response. |
Re: Javascript,html Help by kaykay100(f): 12:18am On Mar 06, 2019 |
stanliwise: Sure,thanks a great deal once again . I understand a lot of tech people don't like to teach all the way ,so for me I appreciate the way you have broken it down for me as a newbie. I understand the concept better. For me the learning continues. |
Re: Javascript,html Help by stanliwise(m): 12:23am On Mar 06, 2019 |
kaykay100:I am glad you're ready to learn. I would help as much as I can. |
(1) (Reply)
Are You Interested In Creating Or Monetizing Your Own Trading Bot Get In Here! / Dear Programmers - Don't Fall For This Scam [pictures] / What's The Best Bulk SMS API In Nigeria
(Go Up)
Sections: politics (1) business autos (1) jobs (1) career education (1) romance computers phones travel sports fashion health religion celebs tv-movies music-radio literature webmasters programming techmarket Links: (1) (2) (3) (4) (5) (6) (7) (8) (9) (10) Nairaland - Copyright © 2005 - 2025 Oluwaseun Osewa. All rights reserved. See How To Advertise. 32 |