+ 2

Javascript Health & Damage

Hello sololearners! I'm still struggling with JScript here, im trying to make a simple battle as an exercise to practice: document.getElementById("Damage").addEventListener("click", attack); let goblinHP = 50; function attack(){ let dPoint = Math.floor((Math.random()*10) + 1); let goblinHP = goblinHP - dPoint; return goblinHP; document.getElementById('textbox').innerHTML = `Goblin received ${dPoint} points of damage!`; document.getElementById('health').innerHTML = `${goblinHP} HP`; } So basically i have an enemy called "goblin", and a button with id "damage" on it, which generates a random between 1 - 10 to deal damage to the "goblin", then there <p> element with id "Health" ( <p id="health">50 HP</p> ) , which i want to change each time i press "attack". I thought my code would work but it doesn't. Am i messing up the scope? Thanks !

24th Apr 2019, 11:17 AM
Mariano Leonel Roura
12 odpowiedzi
+ 3
A) Complete solution: https://code.sololearn.com/WXRjflR2DcxP/?ref=app B) Point by point explanation : 1. Sololearn executes JS tab scripts in head, before the elements exist. Therefore addEventListener and getElementById may return null error. One of the solutions is to wrap scripts in window.onload = function (){ ... } 2. Your have let globinHP already, so don't let it again. Use the variable in global scope directly. We'll talk about closure tomorrow, and let VS var on Friday, follow everything.js 3. Don't need to return in your case. 4. You need to add content, so use += 5. Line break for innerHTMl is <br>(poor way) , line break for innerText is \n. Here I am using \n which have more control on spacing. 6. Take the innerText assignment out as a separate function, for readability.
24th Apr 2019, 12:33 PM
Gordon
Gordon - avatar
+ 5
You are welcome. After reading your description, it is possible that you have use the script tags in your head or beginning of body. This code snippet by Calvin should explain the sequence of JavaScript execution. https://code.sololearn.com/W8qiO41gHNfu/?ref=app If you are importing library, use script at head. If you are coding in the file, use script just before end of body. Lastly here is a link to everything.js https://www.sololearn.com/Profile/13601090/?ref=app We should post about this sequence issue and explain closure and let/var within coming days.
24th Apr 2019, 2:27 PM
Gordon
Gordon - avatar
+ 2
ooh, the function attack end before the textbox updated. move your return statement at the end of the function
24th Apr 2019, 12:03 PM
Taste
Taste - avatar
+ 1
texbox ? the <input type ="text"> ? if it is then dont use .innerHTML but .value
24th Apr 2019, 11:58 AM
Taste
Taste - avatar
0
Sorry, it's a <p> element with the id "health": <p id="health">50 HP</p>
24th Apr 2019, 12:01 PM
Mariano Leonel Roura
0
That function onload wrap works wonders, gordon! Thanks! I could learn a lot by your demonstration code and also could fix mine with that onload (odd thing, i wasnt using solo learn, but my own browser and text editor program, and didnt work before i added the onload wrap)
24th Apr 2019, 2:11 PM
Mariano Leonel Roura
0
Great post! Your explanation of the benefits of daily exercise is very encouraging. It’s amazing how much just 30 minutes of physical activity can impact overall health and mood. Thanks for reminding us how easy it can be to incorporate movement into our routines! https://www.hotfrog.in/company/1201042372276224/dr-ramya-sadaram/visakhapatnam/hospitals-clinics Gynecologist in vizag
3rd Oct 2024, 2:51 PM
Mevish Jabeen
Mevish Jabeen - avatar
0
Fiber is crucial for digestion and helps prevent constipation. Foods rich in fiber, like whole grains, fruits, and vegetables, also promote heart health and maintain stable blood sugar levels. Aim to include more fiber-rich foods in your daily meals to support a healthy digestive system. https://www.lapruebadeadn.com/ Pruebas de ADN
7th Oct 2024, 5:42 AM
Mevish Jabeen
Mevish Jabeen - avatar
0
Thank you for shedding light on mental health awareness. Your post was thoughtful and informative, and I appreciated the encouragement to seek help when needed. Do you have recommendations for daily practices to improve mental well-being? Thanks for promoting such an important topic! https://hartorthodontics.ca orthodontist
12th Dec 2024, 7:15 AM
mohammad mohsin
mohammad mohsin - avatar
0
Regular dental visits ensure healthy teeth and gums. Dentists prevent and treat issues like cavities, gum disease, and oral infections. Early detection of problems saves time, money, and discomfort. Professional cleanings improve oral hygiene, while routine checkups contribute to overall health by identifying links to systemic conditions. https://boostaro.org/ boostaro
30th Dec 2024, 12:49 PM
Mevish Jabeen
Mevish Jabeen - avatar
0
Health is wealth! Prioritizing physical and mental well-being through balanced nutrition, regular exercise, and mindfulness is key to a fulfilling life. Small changes, like staying hydrated, practicing gratitude, and getting enough sleep, can make a big difference. Let's all commit to making healthier choices every day for a better future! https://ohanaphysiotherapy.com.au/ physiotherapist gold coast
31st Dec 2024, 9:29 AM
Mevish Jabeen
Mevish Jabeen - avatar
0
Maintaining good dental health is essential for overall well-being, yet it’s often underestimated. Regular dental check-ups, proper brushing, and flossing can prevent many common issues like cavities and gum disease. It’s always worth investing time and care in your oral hygiene to ensure a healthy and confident smile! <a href="https://denticore-dental.com" target="_blank">Denticore Dental</a>
2nd Jan 2025, 2:30 PM
FemiPro
FemiPro - avatar