Sum of Multiples of 3, 5, and 7

In this lab, you will be working on a function sumOfMultiples which takes a positive integer n and returns the sum of all integers in the range [1, n] inclusive that are divisible by 3, 5, or 7.

You have to ensure your solution follows ESM import/export and is free of bugs. The function and any additional code needed for testing must be exported.

Examples

Example 1:

sumOfMultiples(7); // output: 21

In the range [1, 7], the numbers divisible by 3, 5, or 7 are 3, 5, 6, 7. The sum of these numbers is 21.

Example 2:

sumOfMultiples(10); // output: 40

In the range [1, 10], the numbers divisible by 3, 5, or 7 are 3, 5, 6, 7, 9, 10. The sum of these numbers is 40.

Example 3:

sumOfMultiples(9); // output: 30

In the range [1, 9], the numbers divisible by 3, 5, or 7 are 3, 5, 6, 7, 9. The sum of these numbers is 30.

Constraints

  • 1 <= n <= 103

Challenges

  1. Export the sumOfMultiples function.
  2. Implement the sumOfMultiples function.

Here is the evaluation script you need to complete and include in the final solution:

import fs from 'fs' // testlog is a log of test results const testlog = [] // this first block matches with - Challenge 1 try { const sumOfMultiples = await import('/home/damner/code/index.js').then(t => t.sumOfMultiples) // perform some testing if(typeof sumOfMultiples === 'function') { testlog.push({ status: 'pass' }) } else { throw new Error('sumOfMultiples is not exported as a function') } } catch(error) { testlog.push({ status: 'error', error: error.message || 'Challenge failed' }) } // this second block matches with - Challenge 2 try { const sumOfMultiples = await import('/home/damner/code/index.js').then(t => t.sumOfMultiples) // perform some testing const testCases = [ { input: 7, output: 21 }, { input: 10, output: 40 }, { input: 9, output: 30 } ] testCases.forEach(test => { if(sumOfMultiples(test.input) === test.output) { testlog.push({ status: 'pass' }) } else { throw new Error(`Expected ${test.output} but got ${sumOfMultiples(test.input)}`) } }) } catch(error) { testlog.push({ status: 'error', error: error.message || 'Challenge failed' }) } // very important for the final length of `testlog` array to match the number of challenges, in this case - 2. // write the test log fs.writeFileSync('/home/damner/code/.labtests/testlog.json', JSON.stringify(testlog)) // write the results array boolean. this will map to passed or failed challenges depending on the boolean value at the challenge index fs.writeFileSync(process.env.UNIT_TEST_OUTPUT_FILE, JSON.stringify(testlog.map(result => result.status === 'pass')))

Make sure to keep all rules in mind while completing the lab, including the length of the testlog array, order of try-catch blocks, and other mentioned points.

Adding your container request
Getting your dedicated container
Connecting to your container
Setting up your editor
Finalizing your playground
TerminalEditorBrowser