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
8 Antworten
+ 7
A Simple Explanation of JavaScript Closures
• https://dmitripavlutin.com/simple-explanation-of-javascript-closures/
+ 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
+ 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.
+ 2
Thanks! If you want you could help me again. How I do the globally declaration? 😉
+ 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;
}
+ 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
0
I would know but maybe somethings escape me..... 🅰🅹
Could you check now my code?
https://code.sololearn.com/W0q93CT6yeuE