how to export timeOut variable in JS

I'm making a Js memory game using express and mongoose
function incrementAttempts(){
attempts++;
attemptsHolder.textContent = attempts;

}


// Function to check if the chosen cards match
function checkForMatch() {
const [card1, card2] = chosenCards;
const [cardId1, cardId2] = chosenCardsIds;

// If the cards match, update foundCards count and display
if (card1 === card2) {
foundCards += 2;
foundHolder.textContent = foundCards;
if (foundCards === cardsInGame) {
// const final = attempts;
setTimeout(()=>{
console.log(attempts);
alert(`Congratulations! You found all the pairs in ${attempts} attempts.`);
},0);

}
} else {
// If the cards don't match, flip them back
document.querySelector(`[data-id="${cardId1}"]`).textContent = '';
document.querySelector(`[data-id="${cardId2}"]`).textContent = '';
}

// Clear the chosen cards arrays
chosenCards = [];
chosenCardsIds = [];

// Increase attempts count and display
incrementAttempts()
}
function incrementAttempts(){
attempts++;
attemptsHolder.textContent = attempts;

}


// Function to check if the chosen cards match
function checkForMatch() {
const [card1, card2] = chosenCards;
const [cardId1, cardId2] = chosenCardsIds;

// If the cards match, update foundCards count and display
if (card1 === card2) {
foundCards += 2;
foundHolder.textContent = foundCards;
if (foundCards === cardsInGame) {
// const final = attempts;
setTimeout(()=>{
console.log(attempts);
alert(`Congratulations! You found all the pairs in ${attempts} attempts.`);
},0);

}
} else {
// If the cards don't match, flip them back
document.querySelector(`[data-id="${cardId1}"]`).textContent = '';
document.querySelector(`[data-id="${cardId2}"]`).textContent = '';
}

// Clear the chosen cards arrays
chosenCards = [];
chosenCardsIds = [];

// Increase attempts count and display
incrementAttempts()
}
i want to get hold of the attempts variable in setTimout alert. Can i export it into another js file?? Beacuse mainly i want to push that attempts variable in MonogoDB
3 Replies
dysbulic 🐙
dysbulic 🐙8mo ago
You could pass a callback into checkForMatch like:
function checkForMatch({ onMatch }) {

if(card1 === card2) {
onMatch({ attempts })

function checkForMatch({ onMatch }) {

if(card1 === card2) {
onMatch({ attempts })

Though if you're wanting the value to write it to Mongo, why not just write it where the match is found?
MarkBoots
MarkBoots8mo ago
you could return the current attempts number from the incrementAttempts() after you incremented it and call that as the first line in checkForMatch(), stored in a variable
function incrementAttempts(){
attempts++;
attemptsHolder.textContent = attempts;
return attempts;
}

// Function to check if the chosen cards match
function checkForMatch() {
const [card1, card2] = chosenCards;
const [cardId1, cardId2] = chosenCardsIds;
const attempts = incrementAttempts();
// If the cards match, update foundCards count and display

}
function incrementAttempts(){
attempts++;
attemptsHolder.textContent = attempts;
return attempts;
}

// Function to check if the chosen cards match
function checkForMatch() {
const [card1, card2] = chosenCards;
const [cardId1, cardId2] = chosenCardsIds;
const attempts = incrementAttempts();
// If the cards match, update foundCards count and display

}
JOY
JOY8mo ago
Wanted to have separate files for game logic and backend Guess it was a bad decision I'll try this
Want results from more Discord servers?
Add your server
More Posts