How should I make this more efficient using functions?

  function, javascript

Trying to make this code more efficient using my simulateCatch function.

  if (character === "steve") {
    // STEVE PROBABILITIES: cod (70%), salmon (20%), tropical (5%), puffer (5%)
    let randNum = Math.random();
    if (randNum < 0.7) {
      numCod++;
      codSpanEl.innerHTML = numCod;
      resultImgEl.src = "img/Raw-Cod.png";
    } else if (randNum < 0.9) {
      numSalmon++;
      salmonSpanEl.innerHTML = numSalmon;
      resultImgEl.src = "img/Raw-Salmon.png";
    } else if (randNum < 0.95) {
      numTropical++;
      tropicalSpanEl.innerHTML = numTropical;
      resultImgEl.src = "img/Tropical-Fish.png";
    } else {
      numPuffer++;
      pufferSpanEl.innerHTML = numPuffer;
      resultImgEl.src = "img/Pufferfish.png";
    }
  }

So the end product would look like this:

function catchFish() {
  if (character === "steve") {
    // STEVE PROBABILITIES: cod (70%), salmon (20%), tropical (5%), puffer (5%)
    simulateCatch (0.7, 0.9, 0.95);

How do I code my simulateCatch function?

Source: Ask Javascript Questions

LEAVE A COMMENT