Welcome!

By registering with us, you'll be able to discuss, share and private message with other members of our community.

SignUp Now!
  • Guest, before posting your code please take these rules into consideration:
    • It is required to use our BBCode feature to display your code. While within the editor click < / > or >_ and place your code within the BB Code prompt. This helps others with finding a solution by making it easier to read and easier to copy.
    • You can also use markdown to share your code. When using markdown your code will be automatically converted to BBCode. For help with markdown check out the markdown guide.
    • Don't share a wall of code. All we want is the problem area, the code related to your issue.


    To learn more about how to use our BBCode feature, please click here.

    Thank you, Code Forum.

JavaScript pls help me in coding

Kaun Gayn

Coder
var number = 10
function myFunction1() {
var number = number - 9
}
function myFunction2() {
var number = number + 2
}
if (true) {
document.getElementById("num").innerHTML = number


what is wrong here ?
i used button function
i tapped the button but that element is showing 10
why var number is not 2 added or 9 subtracted?
 
Three big mistakes come to light at first glance.
You don't end command lines with a semicolon ';'. This will get you into trouble someday.
The if(true) has no end bracket.
You never call any of your functions.

AND 4
When you declare a variable outside of a function it's global and can be used anywhere, but in your functions you redefine them.
the number was declared in your first line don't redefine it in the function. cause the formula won't work. var on the right side wipes the 10 from any definition of the number you had and the left side of the equation has a variable (number) which now is blank.

Code:
<body>
<body>
<div id="num1"></div>
<div id="num2"></div>
</body>

<script>
var number = 10;
function myFunction1() {
    number = number - 9;
    document.getElementById("num1").innerHTML = number;
}
function myFunction2() {
    number = number + 2;
    document.getElementById("num2").innerHTML = number;
}
myFunction1();
myFunction2();
</script>
</html>
 

New Threads

Latest posts

Buy us a coffee!

Back
Top Bottom