Newbie functions

How to write this code correctly so that the function result() returns the sum of the var bask1+bask2…
the error is “bask1 is undefined”

function apples(){
    var bask1 = 20;
    var bask2 = 30;
function pears(){
    var bask3 = 40;
    var bask4 = 50;

function result(){
    var total = bask1+bask2+bask3+bask4;

            alert("apples & pears = " + total);


have you went through some course on JS, learning how to declare functions/variables, what scope is etc? Your code is broken, its hard to even tell what you are trying to achieve there. We can put you the right answer, which is nothing complicated, but you would learn nothing and it wont fill the gaps you have in your understanding of functions and variables.

thanks “dad”
you are very helpful

bask1 is undefined because it is scoped inside the apples function

You are also calling both functions inside themselves which you should not be doing in your code

How much experience do you have with JavaScript? Have you followed a course in basic syntax?

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.