Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

258
Views
Bug in code, javascript leetcode 66. Plus One, 71 / 111 test cases passed

This is the question: https://leetcode.com/problems/plus-one/

You are given a large integer represented as an integer array digits, where each digits[i] is the ith digit of the integer. The digits are ordered from most significant to least significant in left-to-right order. The large integer does not contain any leading 0's.

Increment the large integer by one and return the resulting array of digits.

This is my code:

var plusOne = function(digits) {
    let num = 0;
    
    for(let i = 0; i < digits.length; i++){
        num += digits[i] * (10 ** (digits.length-i-1))
    }
    
    return (num+1).toString().split("").map(Number)
}; 

Failed test case:

Input:
[6,1,4,5,3,9,0,1,9,5,1,8,6,7,0,5,5,4,3]
Output:
[6,1,4,5,3,9,0,1,9,5,1,8,6,7,0,5,0,0,0]
Expected:
[6,1,4,5,3,9,0,1,9,5,1,8,6,7,0,5,5,4,4]

I realised that the code also works fine as long as digits.length <= 17, for e.g. [1,2,3, 4, 5, 6, 7, 8, 9, 0, 1, 2, 3, 4, 5, 6, 7] Once there are 18 numbers in digits for e.g. [1,2,3, 4, 5, 6, 7, 8, 9, 0, 1, 2, 3, 4, 5, 6, 7, 8] it stops working and I can't seem to find the bug with this. Can someone please help enlighten me? Thank you!!

about 4 years ago · Juan Pablo Isaza
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!