Welcome, Guest: Register On Nairaland / LOGIN! / Trending / Recent / NewStats: 3,195,337 members, 7,957,901 topics. Date: Wednesday, 25 September 2024 at 12:08 AM |
Nairaland Forum / Science/Technology / Programming / Help Me With This Javascript Problem (1992 Views)
HELP With This Javascript Problem / Help, Javascript Problem, (pictures Attached) (fixed) / Javascript Problem (2) (3) (4)
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: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: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:Since u no solve am |
Re: Help Me With This Javascript Problem by africanman85: 8:50am On Aug 18, 2021 |
Sapeleboy911: |
Re: Help Me With This Javascript Problem by africanman85: 9:00am On Aug 18, 2021 |
Capslock01:Your (if and else augument) did not execute . |
Re: Help Me With This Javascript Problem by LikeAking: 10:30am On Aug 18, 2021 |
Re: Help Me With This Javascript Problem by Capslock01: 11:09am On Aug 18, 2021 |
africanman85: 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 |
Re: Help Me With This Javascript Problem by africanman85: 3:04pm On Aug 18, 2021 |
Capslock01: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:What is the need of using "return statement" when u can log directly to d console with console.log ? |
(1) (Reply)
I Want To Learn Programming On My Own, Where Do I Start? / How To Start A Mobile App Development Business With Little Investment / What Is The Highest Number Output By This Function...
(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. 32 |