A Collection of Code Snippets in as Many Programming Languages as Possible
This project is maintained by TheRenegadeCoder
Welcome to the Maximum Subarray in Javascript page! Here, you'll find the source code for this program as well as a description of how the program works.
function printUsage() {
console.log(
'Usage: Please provide a list of integers in the format: "1, 2, 3, 4, 5"',
);
}
function maxSubarraySum(arr) {
let maxSoFar = -Infinity;
let maxEndingHere = 0;
for (let num of arr) {
maxEndingHere += num;
if (maxSoFar < maxEndingHere) {
maxSoFar = maxEndingHere;
}
if (maxEndingHere < 0) {
maxEndingHere = 0;
}
}
return maxSoFar;
}
function main() {
const args = process.argv.slice(2);
if (args.length < 1 || args[0] === "") {
printUsage();
process.exit(1);
}
const input = args[0];
const arr = input.split(",").map((token) => parseInt(token.trim(), 10));
if (arr.length === 1) {
console.log(arr[0]);
return;
} else if (arr.length === 0) {
printUsage();
process.exit(1);
}
const result = maxSubarraySum(arr);
console.log(result);
}
main();
Maximum Subarray in Javascript was written by:
If you see anything you'd like to change or update, please consider contributing.
No 'How to Implement the Solution' section available. Please consider contributing.
No 'How to Run the Solution' section available. Please consider contributing.