What's going on with the final item in the array?

As I work on the exercise found at , I have devised a solution that has been successfully tested with smaller arrays containing around 5-7 elements.

class ListNode {
    val: number
    next: ListNode | null
    constructor(val?: number, next?: ListNode | null) {
        this.val = (val===undefined ? 0 : val)
        this.next = (next===undefined ? null : next)
    }
}

var l1 = new ListNode(1, new ListNode(0, new ListNode(0, new ListNode(0, new ListNode(0, new ListNode(0, new ListNode(0, new ListNode(0, new ListNode(0, new ListNode(0, new ListNode(0, new ListNode(0, new ListNode(0, new ListNode(0, new ListNode(0, new ListNode(0, new ListNode(0, new ListNode(0 , new ListNode(0, new ListNode(0 , new ListNode(0, new ListNode(1)))))))))))))))))))));
var l2 = new ListNode(5, new ListNode(6, new ListNode(4)));

function addTwoNumbers(l1: ListNode | null, l2: ListNode | null): ListNode | null {
    let r1 = [], r2 = [];
    
    while(l1 !== null)
    {
        r1.push(l1.val);
        l1 = l1.next;
    }
    
    while(l2 !== null)
    {
        r2.push(l2.val);
        l2 = l2.next;
    }

    console.log(r1); console.log(r2);
    
    r1.reverse(); r2.reverse();
    let n1, n2;
    
    n1 = Number(r1.join(''))
    n2 = Number(r2.join(''))
    
    let n3 = n1 + n2;
    let newN3 = n3.toString().split('').reverse();
    
    console.log(`N1: ${n1} N2: ${n2} N3: ${n3} newN3: ${newN3.toString()}`);

    let l3 : ListNode[] = [];
    
    for(let i = 0 ; i < newN3.length ; i ++)
        l3.push(new ListNode(Number(newN3[i]),null));
    
    for(let i = 0 ; i < l3.length ; i++)
        l3[i].next = l3[i+1]??null;

    return l3[0];
};

let salida = addTwoNumbers(l1,l2);
console.log(salida);
while(salida !== null)
{
    console.log('Num; ', salida.val);
    salida = salida.next;
}

Upon further testing with the 21-element array l1, it appears that the last element of the array is being ignored. The output shows:

[
  1, 0, 0, 0, 0, 0, 0,
  0, 0, 0, 0, 0, 0, 0,
  0, 0, 0, 0, 0, 0, 1
]
[ 5, 6, 4 ]
N1: 100000000000000000000 N2: 465 N3: 100000000000000000000 newN3: 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1
ListNode {
  val: 0,
  next: ListNode { val: 0, next: ListNode { val: 0, next: [ListNode] } }
}
Num;  0
Num;  0
Num;  0
Num;  0
Num;  0
Num;  0
Num;  0
Num;  0
Num;  0
Num;  0
Num;  0
Num;  0
Num;  0
Num;  0
Num;  0
Num;  0
Num;  0
Num;  0
Num;  0
Num;  0
Num;  1

It should be noted that N1 should be 100000000000000000001, but it currently outputs as 100000000000000000000. The sum of N1+N2 is also incorrect.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Having trouble with clearInterval in React TypeScript?

I have been encountering issues with the clearInterval function in TypeScript for React. I am not sure why the interval is not being cleared. To address this problem, I defined a variable let interval_counter;, and used it as follows: interval_counter = ...

Tips for Managing Errors During Zip File Extraction in Node.js

Currently, my task involves uploading a zip file, extracting it on the server side, and handling any errors that may occur during the extraction process. I am attempting to achieve this using the following code: var zip = new AdmZip(x); zip.extractAllTo( ...

Angular Translate Directive Unleashes the Power of XSS Vulnerabilities

For my project, I have chosen to utilize the 'escape' method as the sanitization strategy for handling potentially harmful content. This includes implementing the translate directive in certain areas, as well as utilizing the translate filter in ...

Vue.js: Why Objects are Returning 'Undefined' Values in Arrays

Attempting to improve my JavaScript OOP skills, I am also delving into learning vue.js. My current project involves creating a simple task list for practice purposes. While most of it is complete, I have hit a roadblock with the remove task function. Initi ...

Limit the number of API queries allowed in Node.js

In my quest to restrict queries made to the Battle.net API within their limits of 100 calls per second and 36,000 calls per hour, I am facing a challenge. The current implementation of my code is as follows: var async = require ('async'); var b ...

Having issues with narrowing down a TypeScript class

Check out this simplified code snippet: class Thing<InState extends boolean = boolean> { public inState(): this is Thing<true> { return true; } } const y = new Thing(); if (y.inState()) { y } When I hover over y in ...

Experiencing Trouble with Dependency Injection in Angular 5

I'm encountering an error in my angular 5 application ("@angular/core": "5.1.2") and need some assistance. Uncaught Error: Can't resolve all parameters for FooComponent: (?). Service: @Injectable() export class FooService { } Component: @C ...

Experiencing an excessive number of re-renders can be a common issue in React as it has limitations set in place to prevent infinite loops. This

I have integrated React context to access the login function and error from the context provider file for logging into the firebase database. I am trying to display any thrown errors in the app during the login process. However, I encountered an issue whe ...

Associate a click event to a dropdown menu element to trigger on selection

My issue involves having multiple select elements. When one of them is changed, I am trying to bind a click event only to its next element with the .btn class. Below is the code snippet illustrating my problem: <div class="whole"> <ul> ...

Creating a .Net desktop application that utilizes the WebBrowser control and integrates with the Google Maps

I am currently working on a project that involves allowing users to input an address and then click a button. The entered address will be displayed on the WebBrowser control in a .net windows application. I am aware that JavaScript can be executed on the W ...

Looking to extract a particular set of information from past records in a GAS web application

Regarding a previous query I made before (Check for login username and password, and then bring data from every previous entry). I've developed a web application to monitor the working hours of employees at my organization. The app is straightforward ...

The type '(props: Props) => Element' cannot be assigned to the type 'FunctionComponent<FieldRenderProps<any, HTMLElement>>' in React-final-form

I'm fairly new to using TypeScript, and I am currently working on developing a signUp form with the help of React-Final-Form along with TypeScript. Here is the code snippet that describes my form: import React from "react"; import Button from "@mater ...

Error in parsing JSON: An unexpected token < was encountered at the beginning of the JSON input, causing a SyntaxError at position 0 when parsing with

I need to transfer an array from a PHP file to JavaScript and save it in a JavaScript array. Below is the snippet of JavaScript code: xmlhttp = new XMLHttpRequest(); xmlhttp.onreadystatechange=function(){ if (xmlhttp.readyState==4 ...

"Enhancing HTML with jQuery Autocomplete for Advanced Search Results

I am in need of an autocomplete text box for my search feature, and I have decided to implement it using jQuery UI. Utilizing an ASP.NET Core API, I will retrieve search results in JSON format. These search results must be grouped with Bootstrap collapse ...

Steps for referencing a custom JavaScript file instead of the default one:

Currently, I am utilizing webpack and typescript in my single page application in combination with the oidc-client npm package. The structure of the oidc-client package that I am working with is as follows: oidc-client.d.ts oidc-client.js oidc-client.rs ...

Activating the Play button to start streaming a link

Recently delved into the world of Ionic 4 and Angular, so definitely a beginner :) Purchased a UI Template from code canyon but didn't realize I needed to code the music player part. Been trying to get a music stream playing but no luck. Came across ...

`Achieving efficient keyboard navigation with MUI Autocomplete and SimpleBar integration in React``

Currently, I am attempting to integrate the Simplebar scrollbar into the MUI Material Autocomplete component in place of the default browser scrollbar. While everything is functioning correctly, this customization has caused me to lose the ability to use t ...

A step-by-step guide on capturing video for an HTML element

Are there any methods available for recording an HTML element? I know that you can use .captureStream() for canvas elements, but is there a similar option for other HTML elements? Is there a different approach for recording specific parts of a website? ...

AngularJs FileList Drag and Drop Feature

Being brand new to front-end development, I decided it would be a fun challenge to implement drag and drop functionality on an existing upload page. However, as I began integrating ng-flow (a directive that helps with drag and drop), I encountered difficul ...

Effortless Searching with Material UI's AutoComplete Feature

Having trouble with the AutoComplete Material UI component, as it requires pressing Enter twice on form submission. I am looking for a solution that allows me to only select an option once and then be directed to another webpage. Check out the Submission ...