question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Inconsistent results for 'smallestCommons([23, 18])'

See original GitHub issue

Challenge Smallest Common Multiple has an issue. User Agent is: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/52.0.2743.82 Safari/537.36. Please describe how to reproduce this issue, and include links to screenshots if possible.

I’ve tested my code using both Node 6.1.0 on my local machine and in CodePen and it returns the correct results (check the browser console log): https://codepen.io/thoragio/pen/LkgKjA

However, when I run this same code against the test in the challenge, the last test ( smallestCommons([23, 18]) ) fails. When I look at the browser console log, I see that my return value is always a different number somewhere in the 4 million range (e.g., 4841427, 4701970, 4950014), but the correct answer is supposed to be 6056820.

My code:

"use strict"

const smallestCommons = (arr) => {

  let range = []
  let rangeSize = arr[1] > arr[0] ? arr[1] - arr[0] + 1 : arr[0] - arr[1] + 1
  let rangeStart = arr[1] > arr[0] ? arr[0] : arr[1]
  for (let i = 0; i < rangeSize; i++) {
    range.push(rangeStart)
    rangeStart++
  }
  console.log(range)

  let checkSmallest = range[range.length - 1]
  console.log(checkSmallest)

  for (let j = 0; j < range.length; j++) {
    if (checkSmallest % range[j] == 0) {
    }
    else {
      j = -1
      checkSmallest++
    }
  }
  console.log(checkSmallest)
  return checkSmallest
}

smallestCommons([1,5]);

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Comments:7 (6 by maintainers)

github_iconTop GitHub Comments

2reactions
raisedadeadcommented, Aug 19, 2016

@thoragio thanks a lot for the appreciation, and while I agree that your code is correct and I 100% support the fact that as a beginner you are focused on “getting it to work”! Goodjob!

However, to incorporate a corner case like this, we would have to probably brainstorm and churn up a logic that can handle the inconsistent errors like this.

Given the priorities I think this may not be taken up given the complexity it involves. Not to forget that every browser behaves differently.

address it somehow (perhaps just a mention somewhere in the challenge description that inefficient code might fail some of the tests?)

Yes, we can mention that some code might break, but then that would have to do it with all challenges.

Leaving it open for other @FreeCodeCamp/issue-moderators to comment. But I think this can be closed as a wontfix.

1reaction
BKinahancommented, Aug 7, 2016

This code is technically correct in that it outputs the correct values given sufficient time, but it is quite inefficient and inconsistent results in the FCC environment are most likely due to this inefficiency.

For example, incrementing checkSmallest by 1 on each step through the loop when it is not divisible by one of the numbers in the range will make the code take about twenty times longer for the input [23,18] than incrementing by the larger of the two input numbers. (Because the number we are looking for as the smallest common multiple must be a multiple of 23, incrementing by 23 rather than by 1 saves a lot of time.)

That simple change allows this code to pass all tests for this challenge. There are other inefficiencies but this is not the place for further discussion of those.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Inconsistent results for 'smallestCommons([23, 18])' #10118
When I look at the browser console log, I see that my return value is always a different number somewhere in the 4...
Read more >
My passing code getting a fail for tests - JavaScript
When I reached the 'Smallest Common Multiple' lesson, my code that I have tested and shows the correct answers according to the page...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found