+ 1

Why this code doesn't work 😖(web)

I am trying to create a calculator that finds velocity using u+at=v formula but I am facing issues the calculate button is not showing the result https://code.sololearn.com/WDjx1fr0GFoW/?ref=app

22nd Apr 2020, 12:18 PM
Atoms~⚛
Atoms~⚛ - avatar
4 Réponses
+ 1
https://code.sololearn.com/WYW4JmURKZY7/?ref=app just see this we need document.getElementsByClasname("")[0].value since it might return the array so instead if u use document.getElementById("") u would get correct result.
22nd Apr 2020, 1:08 PM
Nikhil Maroju
Nikhil Maroju - avatar
+ 1
Your explanation is good but it does not work I think .It gives error Arvind Singh Rawat.
22nd Apr 2020, 1:20 PM
Nikhil Maroju
Nikhil Maroju - avatar
0
Issues: 1. Your js is executing before your DOM is rendered. Hence, no element is found. 2. Always use getElementById() to get/set values, because of unique target, and for CSS manipulation use getElementsByClass() because you may need to edit multiple classes simultaneously. 3. Always consider undefined cases. function find(){ var a=document.getElementById("ac").value; var iv=document.getElementById("u").value; var ti=document.getElementById("t").value; a= a?a:0; iv= iv?iv:0; ti= ti?ti:0; var velocity= iv+a*ti; console.log(velocity); }
22nd Apr 2020, 1:14 PM
Arvind Singh Rawat
Arvind Singh Rawat - avatar
0
Ohh, I forgot to mention, add corresponding id's in html as classes. For example: change class= "ac" to id="ac" similarly, for ti and u
22nd Apr 2020, 1:22 PM
Arvind Singh Rawat
Arvind Singh Rawat - avatar