i dont understand this js function

good day guys, i google this function and asked gpt but still i dont understand. can someone explain to me?
function array_max_consecutive_sum(nums, k) {
let result = 0;
let temp_sum = 0;
for (var i = 0; i < k - 1; i++) {
temp_sum += nums[i];
}
for (var i = k - 1; i < nums.length; i++) {
temp_sum += nums[i];
if (temp_sum > result) {
result = temp_sum;
}
temp_sum -= nums[i - k + 1];
}
return result;
}
function array_max_consecutive_sum(nums, k) {
let result = 0;
let temp_sum = 0;
for (var i = 0; i < k - 1; i++) {
temp_sum += nums[i];
}
for (var i = k - 1; i < nums.length; i++) {
temp_sum += nums[i];
if (temp_sum > result) {
result = temp_sum;
}
temp_sum -= nums[i - k + 1];
}
return result;
}
3 Replies
get out...!
get out...!16mo ago
as from code nums is the array but please specifiy what is k
Jochem
Jochem16mo ago
where did you get this code? It feels a little like a homework assignment to figure out what it does because that's not terribly useful
get out...!
get out...!16mo ago
I did not find it any where this code is simply add the whole array and returning the result I do not recommend to use that, you can write more efficient code using es6
Want results from more Discord servers?
Add your server