logo CodeStepByStep logo

containsSumSequence

Language/Type: JavaScript arrays
Author: Melissa Hovik (on 2018/10/22)

Write a function containsSumSequence that accepts as parameters an array of Numbers nums and a targetSum and returns true if there is a non-empty sequence of adjacent values in nums that adds to the targetSum. A sequence of adjacent values is one that does not skip any values in the running total.

For example, if a variable called nums stores the following values:

[1, 4, 2, -3, 1, -8]

the call containsSumSequence(nums, 3) would return true since the sequence (4, 2, -3) has a sum of 3 and these are adjacent in the array. The call containsSumSequence(1) would also return true since the first element in the array adds to 1 (it is a sequence of length 1).

The call containsSumSequence(nums, 8) would return false since there is no sequence of adjacent values that sums to 8.

You may assume the given array contains only Number types and is not null.

Type your JavaScript solution code here:


This is a function exercise. Write a JavaScript function as described. Do not write a complete program; just the function(s) above.

You must log in before you can solve this problem.

Log In

Need help?

If you do not understand how to solve an exercise or why your solution doesn't work, please contact your TA or instructor.
If something seems wrong with the site (errors, slow performance, incorrect tests, etc.), please

Is there a problem? Contact a site administrator.

©, all rights reserved.