/    Sign up×
Community /Pin to ProfileBookmark

int to string without need

i am new to JavaScript and i have no idea why the function is not seeing the integer as a number, its suing it as a string and when i load it, it says undefined; help.

[code]const calcAverage = () => {
//ITS A NUMBER HERE
const DolphinScore = (44 + 23 + 71) / 3
const KoalaScore = (65 + 54 + 49) / 3

return

}

calcAverage()

function checkWinner(DolphinScore, KoalaScore) {

const koalaAvg = KoalaScore
const dolphinAvg = DolphinScore

if (dolphinAvg => koalaAvg * 2) { //ITS NOT A NUMBER
console.log(`dolphins win (${dolphinAvg} to ${koalaAvg})`)
} //WHY IS IT A STRING
else if (koalaAvg => dolphinAvg * 2) {
console.log(`koalas win (${dolphinAvg} to ${koalaAvg})`)
}

else {
console.log(`no one wins (${dolphinAvg} to ${koalaAvg})`)
}
return
}

checkWinner()[/code]

to post a comment
JavaScript

7 Comments(s)

Copy linkTweet thisAlerts:
@_email_no_werkauthorFeb 16.2021 — i used back ticks in the code so the entire thing is in like 5 pieces bruh
Copy linkTweet thisAlerts:
@SempervivumFeb 16.2021 — @_email_no_werk#1628087
>i used back ticks in the code so the entire thing is in like 5 pieces bruh

Unfortunately single backtics don't work reliably when posting code. You better use code tags: `your code here` or triple backtics.

I edited your posting accordingly.
Copy linkTweet thisAlerts:
@SempervivumFeb 16.2021 — 
  • 1. DolphinScore and KoalaScore are defined locally in your function calcAverage. They are not available outside this function.

  • 2. You have defined two parameters for the function checkWinner but you don't hand them over when calling it. Therefore these parameters are undefined. My output of console.log reads:

  • >dolphins win (undefined to undefined)
    Copy linkTweet thisAlerts:
    @_email_no_werkauthorFeb 17.2021 — how would i hand them over?
    Copy linkTweet thisAlerts:
    @SempervivumFeb 17.2021 — The way you coded the calls of your functions it's not possible to hand over them as they are not available outside of the function calcAverage. I recommend this code:
    const calcAverage = () => {
    //ITS A NUMBER HERE
    const DolphinScore = (44 + 23 + 71) / 3
    const KoalaScore = (65 + 54 + 49) / 3
    // return object containing both scores for dolphin and koala
    return {dolphin: DolphinScore, koala: KoalaScore};
    }

    function checkWinner() {
    // Get averages
    const averages = calcAverage();
    // Extract average for each animal from object
    const koalaAvg = averages.koala;
    const dolphinAvg = averages.dolphin;

    if (dolphinAvg => koalaAvg * 2) { //ITS NOT A NUMBER
    console.log(<span><code>dolphins win (${dolphinAvg} to ${koalaAvg})</code></span>)
    } //WHY IS IT A STRING
    else if (koalaAvg =&gt; dolphinAvg * 2) {
    console.log(<span><code>koalas win (${dolphinAvg} to ${koalaAvg})</code></span>)
    }

    else {
    console.log(<span><code>no one wins (${dolphinAvg} to ${koalaAvg})</code></span>)
    }
    return
    }

    checkWinner()
    Copy linkTweet thisAlerts:
    @_email_no_werkauthorFeb 17.2021 — thank you smart man, even tho i still have no idea how functions work
    Copy linkTweet thisAlerts:
    @SempervivumFeb 17.2021 — This seems to be a fine resource to learn from how functions work:

    https://codeburst.io/javascript-functions-understanding-the-basics-207dbf42ed99
    ×

    Success!

    Help @_email_no_werk spread the word by sharing this article on Twitter...

    Tweet This
    Sign in
    Forgot password?
    Sign in with TwitchSign in with GithubCreate Account
    about: ({
    version: 0.1.9 BETA 4.26,
    whats_new: community page,
    up_next: more Davinci•003 tasks,
    coming_soon: events calendar,
    social: @webDeveloperHQ
    });

    legal: ({
    terms: of use,
    privacy: policy
    });
    changelog: (
    version: 0.1.9,
    notes: added community page

    version: 0.1.8,
    notes: added Davinci•003

    version: 0.1.7,
    notes: upvote answers to bounties

    version: 0.1.6,
    notes: article editor refresh
    )...
    recent_tips: (
    tipper: @Yussuf4331,
    tipped: article
    amount: 1000 SATS,

    tipper: @darkwebsites540,
    tipped: article
    amount: 10 SATS,

    tipper: @Samric24,
    tipped: article
    amount: 1000 SATS,
    )...