Converting form text in HTML into an array in JS

I am attempting to create an online solver for the maximum subarray problem.
https://en.wikipedia.org/wiki/Maximum_subarray_problem

I planned on taking user-input numbers from a textbox and converting them into an int array in JS, however my JS does not seem to be running at all.
Here is my HTML

<!DOCTYPE html>
<html>
    <head>
        <title> findMaxSum </title>
        <script src="findMaxSum.js" type="text/javascript"></script>
    </head>

    <body>
        <h1> findMaxSum </h1>
        <form id="formarray" action="">
            <p> Enter numbers with spaces, i.e. "1 2 3 4 5": </p>
            <input type="text" id="array"> <br>
            <button id="sum">findMaxSum!</button>
            <br>
        </form>
        <p id="answer">The answer is: </p>
    </body>
</html>

and my JS. note: the map(function(item)) part of the code is intended to break apart the string from the form into an int array.

"use strict";

function findMaxSum() {
    var array = document.getElementById("array").split(" ").map(function(item) {
        return parseInt(item, 10);
    });
    var sumButton = document.getElementById("sum");
    sumButton.onclick = findMaxSum;
    var loopSum = 0;
    var currentMax = 0;
    for (var i = 0; i < array.length; i++) {
        loopSum += array[i];
        if (currentMax < loopSum) {
            currentMax = loopSum;
        } else if (loopSum < 0) {
            loopSum = 0;
        }
    }
    document.getElementById("answer").innerHTML = "The answer is: " + currentMax;
}

window.onload = findMaxSum;

Currently, when I type in numbers into the textbox and submit, the numbers disappear and nothing happens. Any help is greatly appreciated.


Source: forms

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.