Welcome, Guest: Register On Nairaland / LOGIN! / Trending / Recent / New
Stats: 3,148,563 members, 7,801,590 topics. Date: Thursday, 18 April 2024 at 05:45 PM

Help Me With This Javascript Problem - Programming - Nairaland

Nairaland Forum / Science/Technology / Programming / Help Me With This Javascript Problem (1787 Views)

HELP With This Javascript Problem / Help, Javascript Problem, (pictures Attached) (fixed) / Javascript Problem (2) (3) (4)

(1) (Reply) (Go Down)

Help Me With This Javascript Problem by africanman85: 9:17pm On Aug 17, 2021
I am new to javascript and finding it difficult to solve this problem with 'javascript function expression'. I can use arrow function. The problem is as bellow:
Back to the two gymnastics teams, the Dolphins and the Koalas! There is a new
gymnastics discipline, which works differently.
Each team competes 3 times, and then the average of the 3 scores is calculated (so
one average score per team).
A team only wins if it has at least double the average score of the other team.
Otherwise, no team wins!
Your tasks:
1. Create an arrow function 'calcAverage' to calculate the average of 3 scores
2. Use the function to calculate the average for both teams
3. Create a function 'checkWinner' that takes the average score of each team
as parameters ('avgDolhins' and 'avgKoalas'), and then logs the winner
to the console, together with the victory points, according to the rule above.
Example: "Koalas win (30 vs. 13)"
4. Use the 'checkWinner' function to determine the winner for both Data 1 and
Data 2
5. Ignore draws this time
Test data:
§ Data 1: Dolphins score 44, 23 and 71. Koalas score 65, 54 and 49
§ Data 2: Dolphins score 85, 54 and 41. Koalas score 23, 34 and 27

1 Like

Re: Help Me With This Javascript Problem by etoluw: 9:49pm On Aug 17, 2021
let CalcAverage = (a,b,c) => (a+b+c)/3;
checkWinner = (a,b) => a > 2 * b;
Re: Help Me With This Javascript Problem by africanman85: 10:59pm On Aug 17, 2021
etoluw:
let CalcAverage = (a,b,c) => (a+b+c)/3;

checkWinner = (a,b) => a > 2 * b;
U used arrow function. I need it to be in function expression.
Something like this format: const CalcAverage = function (a, b, c)
Re: Help Me With This Javascript Problem by Capslock01: 12:47am On Aug 18, 2021
const calcAverage1 = function (dolphins1, dolphins2, dolphins3) {
const avgDolphins = (dolphins1 + dolphins2 + dolphins3)/3;
console.log('Dolphins Average is ---> ' + avgDolphins)
}
avgDolphins = calcAverage1 (44, 23, 71); //Test for Data 1 --- "Value is 46"

const calcAverage2 = function (Koalas1, Koalas2, Koalas3) {
const avgKoalas = (Koalas1 + Koalas2 + Koalas3)/3;
console.log('Koalas Average is ---> '+ avgKoalas)
}
avgKoalas = calcAverage2 (65, 54, 49); //Test for Data 2 --- "Value is 56"

//This code takes Average as parameters
function checkWinner (avgDolphins, avgKoalas) {
if (avgDolphins >= 2*avgKoalas) {
console.log (`Delphins win (${avgDolphins} vs ${avgKoalas})`);
} else if (avgKoalas >= 2*avgDolphins) {
console.log (`Koalas win (${avgDolphins} vs ${avgKoalas})`);
} else {
console.log('No team win')
}
}

checkWinner(46, 56) //Test for Data 1 Average



I hope this help. I will try to help you to refine the code tomorrow.

2 Likes

Re: Help Me With This Javascript Problem by africanman85: 1:17am On Aug 18, 2021
Capslock01:
const calcAverage1 = function (dolphins1, dolphins2, dolphins3) {
const avgDolphins = (dolphins1 + dolphins2 + dolphins3)/3;
console.log('Dolphins Average is ---> ' + avgDolphins)
}
avgDolphins = calcAverage1 (44, 23, 71); //Test for Data 1 --- "Value is 46"

const calcAverage2 = function (Koalas1, Koalas2, Koalas3) {
const avgKoalas = (Koalas1 + Koalas2 + Koalas3)/3;
console.log('Koalas Average is ---> '+ avgKoalas)
}
avgKoalas = calcAverage2 (65, 54, 49); //Test for Data 2 --- "Value is 56"

//This code takes Average as parameters
function checkWinner (avgDolphins, avgKoalas) {
if (avgDolphins >= 2*avgKoalas) {
console.log (`Delphins win (${avgDolphins} vs ${avgKoalas})`);
} else if (avgKoalas >= 2*avgDolphins) {
console.log (`Koalas win (${avgDolphins} vs ${avgKoalas})`);
} else {
console.log('No team win')
}
}

checkWinner(46, 56) //Test for Data 1 Average



I hope this help. I will try to help you to refine the code tomorrow.
Ok sir
Re: Help Me With This Javascript Problem by Sapeleboy911(m): 6:52am On Aug 18, 2021
function calcAverage(value1, value2, value3){
return (value1 + value2 + value3) / 3;
};

// The code above calculates the average of any 3 given numbers.

let avgDolphins = calcAverage(10, 15, 20);

let avgKoalas = calcAverage(30, 40, 20);

//The code above is calling the calcAverage function and assigning it to variables.


function checksWinner(avgDolphins, avgKoalas){

if(avgDolphins >= 2 * avgKoalas){

return `Dolphins wins (${avgDolphins} VS ${avgKoalas}) `;

}else if(avgKoalas >= 2 * avgDolphins){
return (`Koalas Wins (${avgKoalas} VS ${avgDolphins})`);

}else{
return "No winner";
}
}


You can use console.log instead of using different return statements in one function. It's just a bad habit of mine. This must be Jonas Schmedtmann's course on udemy, I'm a student of the course too. Just rounded up the loop section.
Re: Help Me With This Javascript Problem by LikeAking: 8:31am On Aug 18, 2021
small tin
Re: Help Me With This Javascript Problem by africanman85: 8:36am On Aug 18, 2021
LikeAking:
small tin
Since u no solve am grin
Re: Help Me With This Javascript Problem by africanman85: 8:50am On Aug 18, 2021
Sapeleboy911:
function calcAverage(value1, value2, value3){
return (value1 + value2 + value3) / 3;
};

// The code above calculates the average of any 3 given numbers.

let avgDolphins = calcAverage(10, 15, 20);

let avgKoalas = calcAverage(30, 40, 20);

//The code above is calling the calcAverage function and assigning it to variables.


function checksWinner(avgDolphins, avgKoalas){

if(avgDolphins >= 2 * avgKoalas){

return `Dolphins wins (${avgDolphins} VS ${avgKoalas}) `;

}else if(avgKoalas >= 2 * avgDolphins){
return (`Koalas Wins (${avgKoalas} VS ${avgDolphins})`);

}else{
return "No winner";
}
}


You can use console.log instead of using different return statements in one function. It's just a bad habit of mine. This must be Jonas Schmedtmann's course on udemy, I'm a student of the course too. Just rounded up the loop section. Your code is returning nothing in the console. Re-check your code. Your last calibrace, i dont know the function is holding.
Re: Help Me With This Javascript Problem by africanman85: 9:00am On Aug 18, 2021
Capslock01:
const calcAverage1 = function (dolphins1, dolphins2, dolphins3) {
const avgDolphins = (dolphins1 + dolphins2 + dolphins3)/3;
console.log('Dolphins Average is ---> ' + avgDolphins)
}
avgDolphins = calcAverage1 (44, 23, 71); //Test for Data 1 --- "Value is 46"

const calcAverage2 = function (Koalas1, Koalas2, Koalas3) {
const avgKoalas = (Koalas1 + Koalas2 + Koalas3)/3;
console.log('Koalas Average is ---> '+ avgKoalas)
}
avgKoalas = calcAverage2 (65, 54, 49); //Test for Data 2 --- "Value is 56"

//This code takes Average as parameters
function checkWinner (avgDolphins, avgKoalas) {
if (avgDolphins >= 2*avgKoalas) {
console.log (`Delphins win (${avgDolphins} vs ${avgKoalas})`);
} else if (avgKoalas >= 2*avgDolphins) {
console.log (`Koalas win (${avgDolphins} vs ${avgKoalas})`);
} else {
console.log('No team win')
}
}

checkWinner(46, 56) //Test for Data 1 Average



I hope this help. I will try to help you to refine the code tomorrow.
Your (if and else augument) did not execute .
Re: Help Me With This Javascript Problem by LikeAking: 10:30am On Aug 18, 2021
africanman85:
Since u no solve am grin



I just tire to solve am. Na small thing.
Re: Help Me With This Javascript Problem by Capslock01: 11:09am On Aug 18, 2021
africanman85:

Your (if and else augument) did not execute .

I executed it on my PC and it ran. Probably the "avgDolphins" and "avgKoalas" has already been declared in your local variable, if it was declared in the global variable you would have gotten error.

I modified the code already and I tested it.
It worked.

you can check below:

//Q1
const calcAvg = function (input1, input2, input3){
return (input1 + input2 + input3)/3;
}

//Q2 ---DATA 1 && DATA 2
const avgDolphins = calcAvg(44, 23, 71); //Test for Data 1 --- "Value is 46"
const avgKoalas = calcAvg(65, 54, 49); //Test for Data 2 --- "Value is 56"

const avgDolphins1 = calcAvg(85, 54, 41); //Test for Data 1 --- "Value is 60"
const avgKoalas1 = calcAvg(23, 34, 27); //Test for Data 2 --- "Value is 28"

console.log('Dolphins Average is ---> ' + avgDolphins)
console.log('Koalas Average is ---> '+ avgKoalas)

console.log('Dolphins Average is ---> ' + avgDolphins1)
console.log('Koalas Average is ---> '+ avgKoalas1)

//Q3 && Q5 --- IF statement covers Q5 ---
function checkWinner (avgDolphins1, avgKoalas1) {
if (avgDolphins1 >= 2*avgKoalas1) {
console.log (`Delphins win (${avgDolphins1} vs ${avgKoalas1})`);
} else if (avgKoalas1 >= 2*avgDolphins1) {
console.log (`Koalas win (${avgDolphins1} vs ${avgKoalas1})`);
} else {
console.log('No team win')
}
}
//Q4 && Q5
checkWinner(46, 56) //Test for Data 1 Average
checkWinner(60, 28) //Test for Data 1 Average


You can post more questions/challenge
Re: Help Me With This Javascript Problem by africanman85: 3:04pm On Aug 18, 2021
Capslock01:


I executed it on my PC and it ran. Probably the "avgDolphins" and "avgKoalas" has already been declared in your local variable, if it was declared in the global variable you would have gotten error.

I modified the code already and I tested it.
It worked.

you can check below:



You can post more questions/challenge
Using "Return" confuses me
Re: Help Me With This Javascript Problem by Sapeleboy911(m): 6:06pm On Aug 18, 2021
[quote author=africanman85 post=104915326][/quote]

The code will not be executed, because the checksWinner function has not been called. If you use console.log to call the checksWinner function the code will be executed. The last curly brace is the closing brace for the checkWinner function.
Re: Help Me With This Javascript Problem by africanman85: 4:48pm On Aug 19, 2021
Capslock01:


I executed it on my PC and it ran. Probably the "avgDolphins" and "avgKoalas" has already been declared in your local variable, if it was declared in the global variable you would have gotten error.

I modified the code already and I tested it.
It worked.

you can check below:



You can post more questions/challenge
What is the need of using "return statement" when u can log directly to d console with console.log ?

(1) (Reply)

Virtual Reality On A Website / Build Android And IOS App With Python. / How To Create A Balanced Binary Tree-cracked

(Go Up)

Sections: politics (1) business autos (1) jobs (1) career education (1) romance computers phones travel sports fashion health
religion celebs tv-movies music-radio literature webmasters programming techmarket

Links: (1) (2) (3) (4) (5) (6) (7) (8) (9) (10)

Nairaland - Copyright © 2005 - 2024 Oluwaseun Osewa. All rights reserved. See How To Advertise. 36
Disclaimer: Every Nairaland member is solely responsible for anything that he/she posts or uploads on Nairaland.