Execute a function and use the output later in my script without calling the function again – Javascript [closed]

Tags:



I want to execute a function and use the output later in my script without calling the function again. I’m trying to use the output of deal().

let playersHand = ["Ace of Clubs", "6 of Hearts"];

function deal(){
  let cardPosition = Math.floor(Math.random()*(shuffledDeck.length));
  dealCard = shuffledDeck.splice(cardPosition, 1);
  return dealCard;
}
function allOfPlayersCards(){
  let playersCards = playersHand.concat(deal);
  console.log(playersCards)
}

The Console Output is ["Ace of Clubs", "6 of Hearts", ƒ]

If I do deal() it executes deal again, and I use deal the output just says ‘f’.

Thank you, sorry if this is obvious I’m pretty new to coding.

Answer

Store the result of deal() on a variable and reuse the variable instead of calling it again (unless if you want to do calculation again)



Source: stackoverflow