Home > OS >  JavaScript function won't run twice with different inputs
JavaScript function won't run twice with different inputs

Time:11-25

I'm trying to make this function run twice with two different inputs, but it only runs once.

the code:

const data1dolphins = 44   23   71;
const data1Koalas = 65   54   49;
const data2dolphins = 85   54   41;
const data2Koalas = 23   34   27;

function calcAverage(data, dataValue) {
  const scoreAverage = data * dataValue
  return scoreAverage;
}

const data1DolphinsAverage = calcAverage(data1dolphins, 3)
const data1KoalasAverage = calcAverage(data1Koalas, 3)
const data2DolphinsAverage = calcAverage(data2dolphins, 3)
const data2KoalasAverage = calcAverage(data2Koalas, 3)

function checkWinner(avgTeamOne, avgTeamTwo) {
  if (avgTeamOne >= (avgTeamTwo * 2)) {
    console.log(`team one won with average score of : ${avgTeamOne}, while team two lost with average score of : ${avgTeamTwo}`)
  } else if (avgTeamTwo >= (avgTeamOne * 2)) {
    console.log(`team two won with average score of : ${avgTeamTwo}, while team one lost with average score of : ${avgTeamOne}`)
  }
  // console.log('testing round')
}

console.log('before')
checkWinner(data1DolphinsAverage, data1KoalasAverage)
console.log('middle')
checkWinner(data2DolphinsAverage, data2KoalasAverage)
console.log('after')
<iframe name="sif1" sandbox="allow-forms allow-modals allow-scripts" frameborder="0"></iframe>

the output:

before

team one won with average score of : 540, while team two lost with average score of : 252

middle

after

CodePudding user response:

Have you tried an else statement?

function checkWinner (avgTeamOne,avgTeamTwo){
    if(avgTeamOne >= (avgTeamTwo*2)) {
        console.log(`team one won with average score of : ${avgTeamOne}, while team two lost with average score of : ${avgTeamTwo}`);
    } else if (avgTeamTwo >= (avgTeamOne*2)){
        console.log(`team two won with average score of : ${avgTeamTwo}, while team one lost with average score of : ${avgTeamOne}`);
    } else { <<
        console.log("neither of these things happened."); <<
    } <<
}

I find it a bit hard to see your purpose, as you didn't provide values for data1dolphins, data1Koalas, data2dolphins, or data2Koalas, but I'm guessing that this is the fix that you need.

CodePudding user response:

the function ran fine after adding an else statment:

const data1dolphins = 44 23 71;
const data1Koalas = 65 54 49;
const data2dolphins = 85 54 41;
const data2Koalas = 23 34 27;

function calcAverage (data,dataValue){
  const scoreAverage =  data * dataValue
  return scoreAverage;
}

const data1DolphinsAverage = calcAverage(data1dolphins,3)
const data1KoalasAverage = calcAverage(data1Koalas,3)
const data2DolphinsAverage = calcAverage(data2dolphins,3)
const data2KoalasAverage = calcAverage(data2Koalas,3)

function checkWinner (avgTeamOne,avgTeamTwo){
  if(avgTeamOne >= (2*avgTeamTwo)) {
    console.log(`team one won with average score of : ${avgTeamOne}, while team two lost with average score of : ${avgTeamTwo}`)
  } else if (avgTeamTwo >= (2*avgTeamOne)){
    console.log(`team two won with average score of : ${avgTeamTwo}, while team one lost with average score of : ${avgTeamOne}`)
  }else {
    console.log('none of the above')
  }
}

console.log('before')
checkWinner(data1DolphinsAverage, data1KoalasAverage)
console.log('middle')
checkWinner(data2DolphinsAverage, data2KoalasAverage)
console.log('after')
  • Related