Variable declaration [Solved] | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

Variable declaration [Solved]

I would put on the result id the sum of the weight and the price, but i can't do it!!! Please someone could help me? This is the web code https://code.sololearn.com/W0q93CT6yeuE

13th Feb 2020, 12:17 PM
Simone Cavalletti
Simone Cavalletti - avatar
8 Answers
+ 7
A Simple Explanation of JavaScript Closures • https://dmitripavlutin.com/simple-explanation-of-javascript-closures/
13th Feb 2020, 2:27 PM
Danijel Ivanović
Danijel Ivanović - avatar
+ 3
Check line no 83. You are accessing local variable as global variable which is declared in this function addWeight1() locally. You have to declared it globally. Same for price1 also update results to result on line number 79
13th Feb 2020, 12:40 PM
A͢J
A͢J - avatar
+ 3
Simone Cavalletti Let me give you example. 1 - for local variable function getPrice() { var price = 0; // this is called local variable } 2 - for global variable declaration:- var price = 0; // this is called as global variable which can be access anywhere. function getPrice() { price = 10 $; } global variable can be access in other functions also like function calculateAmount() { return price * price; } So finally when you declare variable inside the function that is called local variable and when you declare variable outside the function that is called global variable. I hope you understood now.
13th Feb 2020, 12:53 PM
A͢J
A͢J - avatar
+ 2
Thanks! If you want you could help me again. How I do the globally declaration? 😉
13th Feb 2020, 12:46 PM
Simone Cavalletti
Simone Cavalletti - avatar
+ 2
Simone Cavalletti Update your code with this. function result() { var weight1= 0; var price1= 0; weight1= document.getElementById("Weight1").innerHTML; price1= document.getElementById("Price1").innerHTML; result = weight1 * price1; document.getElementById("result").innerHTML = result; }
13th Feb 2020, 4:51 PM
A͢J
A͢J - avatar
+ 1
Finally I solved!! Maybe for someone was a simple thing, but I am a newbie!!!! So this is my final code!! https://code.sololearn.com/W0q93CT6yeuE
13th Feb 2020, 5:05 PM
Simone Cavalletti
Simone Cavalletti - avatar
0
I would know but maybe somethings escape me..... 🅰🅹 Could you check now my code? https://code.sololearn.com/W0q93CT6yeuE
13th Feb 2020, 4:26 PM
Simone Cavalletti
Simone Cavalletti - avatar