I create a function buttonEffects()
with four button effects (one effect for each). Therefore, I would like to know how can I bring these variables outside of this function and fetch them into another function? I don’t know whether I am able to do that, or I will need to recreate my code.!?
I already return each of the var on the bottom of the function buttonEffects()
such as return (var1, var2, var3, var4);
and declare them outside of it but with no success.
//jQuery
$(document).ready(function() {
let blueBtnAudio = new Audio('https://s3.amazonaws.com/freecodecamp/simonSound1.mp3');
let redBtnAudio = new Audio('https://s3.amazonaws.com/freecodecamp/simonSound2.mp3');
let yellowBtnAudio = new Audio('https://s3.amazonaws.com/freecodecamp/simonSound3.mp3');
let greenBtnAudio = new Audio('https://s3.amazonaws.com/freecodecamp/simonSound4.mp3');
// VARIABLES - DOM QUERIES
const btnBlue = "#btnBlue";
const btnGreen = "#btnGreen";
const btnRed = "#btnRed";
const btnYellow = "#btnYellow";
const startButton = "#startButton";
const randomColors = ['blueButtonEffect', 'greenButtonEffect', 'redButtonEffect', 'yellowButtonEffect'];
console.log('blueButtonEffect');
//button effects
function buttonEffects() {
//button blue effect
var blueButtonEffect = $(btnBlue).click(function() {
var originalColor = $(this).css('background-color');
blueBtnAudio.play();
$(this).css('background-color', '#00FFFF');
setTimeout(function() {
$(btnBlue).css('background-color', originalColor);
}, 100);
});
//button green effect
var greenButtonEffect = $(btnGreen).click(function() {
var originalColor = $(this).css('background-color');
greenBtnAudio.play();
$(this).css('background-color', '#7FFF00');
setTimeout(function() {
$(btnGreen).css('background-color', originalColor);
}, 100)
});
// button red effect
var redButtonEffect = $(btnRed).click(function() {
var originalColor = $(this).css('background-color');
redBtnAudio.play();
$(this).css('background-color', '#F08080');
setTimeout(function() {
$(btnRed).css('background-color', originalColor)
}, 100);
});
// button yellow effect
var yellowButtonEffect = $(btnYellow).click(function() {
var originalColor = $(this).css('background-color');
yellowBtnAudio.play();
$(this).css('background-color', '#F0E68C');
setTimeout(function() {
$(btnYellow).css('background-color', originalColor)
}, 100);
});
}
// start the game
function startGame() { // it has a bug if clicked twice!
$(startButton).on('click', buttonEffects);
};
startGame();
function changeColor() {
//some code here?
}
});
I am trying a new approach in which I return all variables into an array and try to bring it back out of the function with this approach:
// In this sample I just bring the relevant part of the code:
//button effects
function buttonEffects() {
//button blue effect
var blueButtonEffect = $(btnBlue).click(function() {
var originalColor = $(this).css('background-color');
blueBtnAudio.play();
$(this).css('background-color', '#00FFFF');
setTimeout(function() {
$(btnBlue).css('background-color', originalColor);
}, 100);
});
//button green effect
var greenButtonEffect = $(btnGreen).click(function() {
var originalColor = $(this).css('background-color');
greenBtnAudio.play();
$(this).css('background-color', '#7FFF00');
setTimeout(function() {
$(btnGreen).css('background-color', originalColor);
}, 100)
});
// button red effect
var redButtonEffect = $(btnRed).click(function() {
var originalColor = $(this).css('background-color');
redBtnAudio.play();
$(this).css('background-color', '#F08080');
setTimeout(function() {
$(btnRed).css('background-color', originalColor)
}, 100);
});
// button yellow effect
var yellowButtonEffect = $(btnYellow).click(function() {
var originalColor = $(this).css('background-color');
yellowBtnAudio.play();
$(this).css('background-color', '#F0E68C');
setTimeout(function() {
$(btnYellow).css('background-color', originalColor)
}, 100);
});
return [blueButtonEffect, redButtonEffect, greenButtonEffect, yellowButtonEffect];
}
var effects = buttonEffects();
var blueButtonEffect = effects[0];
var redButtonEffect = effects[1];
var greenButtonEffect = effects[2];
var yellowButtonEffect = effects[3];
console.log(effects);
// start the game
function startGame() { // it has a bug if clicked twice!
$(startButton).on('click', buttonEffects);
};
startGame();
function changeColor() {
//bodyBcg.style.backgroundColor = colors[2];
let random = Math.floor(Math.random() * randomColors.length)
}