Smallest Common Multiple

hello ,
there is this line i could not understand it what the meaning of it
“.map((_, i) => i + min);”
could anyone help me with that ?

Can you share your solution to this problem? Its easier to compare a code you understand and one you don’t.

1 Like

this is the full solution from solution section , i struggle on that line .thank you

function smallestCommons(arr) {
  // Setup
  const [min, max] = arr.sort((a, b) => a - b);
  const range = Array(max - min + 1)
    .map((_, i) => i + min);
  // Largest possible value for SCM
  const upperBound = range.reduce((prod, curr) => prod * curr);
  // Test all multiples of 'max'
  for (let multiple = max; multiple <= upperBound; multiple += max) {
    // Check if every value in range divides 'multiple'
    const divisible = range.every((value) => multiple % value === 0);
    if (divisible) {
      return multiple;

smallestCommons([1, 5]);

Please share the solution that you wrote so we can compare them. Its much, much harder to explain without a solution of your own.

i could not write any line ,still in the process of learning .it,s very hard to come up with complex syntax at this stage , i try to understand the solution to learn more .

Reading other people’s answers won’t help you learn how to write your own answers. Reading code is an important skill, but its a different skill than writing code.

For this challenge, it’s important to remember that you aren’t required to re-invent the math for determining the smallest common multiple. If you google around, you can find a few different formulas (I thought the Wikipedia article was helpful). Then you can work on translating that into a JavaScript algorithm.

how i,m going to write syntax that i don’t know .i’m learning by doing .i have to solve and see a lot of exercices .no problem ,don’t bother i’ll understand it :wink: thnx

i’m want to understand only one line ““.map((_, i) => i + min);”” is it putting index for an array ?or what thanks ?

You don’t need any fancy syntax. You can solve this problem with only syntax you have covered in the previous challenges. That’s how Solution 1 is written.

Copying somebody else’s answer isn’t “doing”. Writing your own answer is “doing”. Its far more important to try to write an answer yourself than to find answers to copy. You cannot copy answers for the projects.

The point of these exercises is not to get the answer. The point is to practice making code to accomplish a task. The exact solution code used is fairly meaningless here. The process of writing that solution code is what matters.

i did not say that i’m copying the solution !! i told you that the line i did not understand is in this piece of code !!

You said you haven’t written a solution yet. You should not look at the solutions until you have written your own.

Looking at the solutions before you write your own means you lose the chance to actually write a solution yourself. You’re taking away a chance to practice from yourself.

If you want to learn more about .map() and it’s callback function, the MDN documentation might be a good place to start.

yeah that,s what i said , i did not say that i,m copying !! why shouldn’t i look if there is a hint option ?!

You shouldn’t look at the solutions yet because…

The solutions are hidden on that page for that very reason. The hints are not hidden while the solutions are.

thank you very much , i know the map() function ,what inside it is still confusing .

If it’s the underscore that you’re stuck on, underscores are used to basically ignore the parameter of a function.

In this example:

Normally you have .map((value, index)=>index + min)
Value isn’t used so you can get just the index with .map((_, index)…)

Obviously i is short for index.

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.