0
function listacursos(curso1,curso2,curso3){
    let curso1=[{
        Ide: 154532,
        nombre:'marketing digital basico',
        duracion: '15 dias',
        valor: 100000,

    }];
        let curso2=[{
        Ide: 154533,
        nombre:'diseño grafico basico',
        duracion: '30 dias',
        valor: 200000,
    }];
        let curso2={
        Ide: 154534,
        nombre:'animacion digital',
        duracion:'35 dias',
        valor: 180000,
    };  
}
    setInterval(function(listacursos){
        console.log('el nombre del curso es: '+curso2.nombre)
     },3000);**strong text**

I am trying to create a program that lists me every variable course in the function "listacursos" but I see error as if the variable was not being declared, I have tried everything because it has to show a message with the information of each course in a period of time of 2 seconds, I know how to do the time delay but I can not make the impression with the console.log.

This is the error that the cmd shows me enter image description here

2 Answers 2

1

The reason you can't access the variables inside of your listacursos function is because of variable scoping.

In Javascript there are two types of variables; variables declared inside of functions are locally-scoped meaning they can only be accessed within that function:

function example(){
   let foo = "test1";
   console.log(foo); // Fine!
}
console.log(foo); // Raises an Error

And globally-scoped, variables declared outside of a function, which can be accessed everywhere:

const foo = "test1";
function example(){
   console.log(foo); // Fine!
}
console.log(foo); // Fine!

This is a somewhat simplified explanation, but there is a more in-depth explanation here.

Sign up to request clarification or add additional context in comments.

Comments

0

The answer by Derek Brown is correct by I will add more precision to your code

function listacursos(curso1,curso2,curso3){
    let curso1=[{
        Ide: 154532,
        nombre:'marketing digital basico',
        duracion: '15 dias',
        valor: 100000,

    }];
        let curso2=[{
        Ide: 154533,
        nombre:'diseño grafico basico',
        duracion: '30 dias',
        valor: 200000,
    }];
        let curso2={ //You already declare a "curso2" variable 
        Ide: 154534,
        nombre:'animacion digital',
        duracion:'35 dias',
        valor: 180000,
    };  
}
    setInterval(function(listacursos){ // Your function 'listacurso' doesn't return anything, so you are just executing it.
        console.log('el nombre del curso es: '+curso2.nombre)
     },3000);

In order to make it work I would suggest that you do something like

    function listacursos(){
        let obj = {};
        let curso1=[{
        Ide: 154532,
        nombre:'marketing digital basico',
        duracion: '15 dias',
        valor: 100000,

    }];
        let curso2=[{
        Ide: 154533,
        nombre:'diseño grafico basico',
        duracion: '30 dias',
        valor: 200000,
    }];
        let curso3={
        Ide: 154534,
        nombre:'animacion digital',
        duracion:'35 dias',
        valor: 180000,
    };
    obj.curso1 = curso1;
    obj.curso2 = curso2;
    obj.curso3 = curso3;
    return obj;
}
    setInterval(function(){
        let courso = listacursos();
        console.log('el nombre del curso es: ', courso.curso2[0].nombre)
     },3000);

Which means that you store every "curso" in an "obj" a JS JSON to be more precise and you return it. Then you create a variable to store that JSON, the "curso" variable.

As "curso2" is actually an array you have to add "[0]" in order to read the JSON inside.

You can fond more about JSON here : https://www.w3schools.com/js/js_json.asp

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.