This is my code:
function test(tlm) {
var soundtimer = 'success';
var type = tlm.replace('send','');
var timer = window[type+'timer'];
console.log(timer);
}
test('sendsound');
I should get success but I get undefined instead. Why is that?
This is my code:
function test(tlm) {
var soundtimer = 'success';
var type = tlm.replace('send','');
var timer = window[type+'timer'];
console.log(timer);
}
test('sendsound');
I should get success but I get undefined instead. Why is that?
Your 'soundtimer' variable is locally scoped to the test function. Moving it outside allows it to be globally declared on window:
var soundtimer = 'success';
function test(tlm) {
var type = tlm.replace('send','');
var timer = window[type+'timer'];
console.log(timer);
}
test('sendsound');
You can use an object within your function.
function test(tlm) {
var local = {
soundtimer:'success'
};
var type = tlm.replace('send','');
var timer = local[type+'timer'];
console.log(timer);
}
test('sendsound');