Repository navigation
#264 Coding Website Score Calculator
Create a function that takes the amount of challenges a user has completed for each challenge level,
and calculates the user's total number of points. Keep in mind that a user cannot complete negative challenges,
so the function should return the string "invalid" if any of the passed parameters are negative.
Current source and tests | Catalog | Testing and tooling
From the cloned repository root, after installing dependencies:
node "Edabit/264 Coding Website Score Calculator.js"Assertions are kept alongside the implementation and throw on failure. A successful run exits with code 0. Run every exercise with npm test or both lint and tests with npm run check.
This is the complete executable file, including its imports and inline test cases. Run it from the repository, not as an isolated file copied out of the wiki. Line endings and trailing whitespace are normalized; executable tokens are unchanged.
//Create a function that takes the amount of challenges a user has completed for each challenge level,
//and calculates the user's total number of points. Keep in mind that a user cannot complete negative challenges,
//so the function should return the string "invalid" if any of the passed parameters are negative.
function scoreCalculator(easy, med, hard) {
return (easy < 0 || med < 0 || hard < 0) ? "invalid" : (easy * 5 + med * 10 + hard * 20)
}
import { assertEquals } from '../testHelper.js';
assertEquals(scoreCalculator(4, 2, 7), 180, "Test 1")
assertEquals(scoreCalculator(3, 12, 0), 135, "Test 2")
assertEquals(scoreCalculator(5, -5, 2), "invalid", "Test 3")
assertEquals(scoreCalculator(20, 7, 3), 230)
assertEquals(scoreCalculator(0, 0, 0), 0)
assertEquals(scoreCalculator(1, 2, 3), 85)