id/email
password
forgot password | create account
about | help | code help+videos | done | prefs
CodingBat code practice

 

addSubtractArray37


Write a method that finds the "total" of an array of integers. The "total" is found by adding the elements of the array until a 37 is encountered. The 37 acts as a toggle, so after the first occurance of a 37, the next numbers are SUBTRACTED from the total until another 37 is encountered. Then it switches back to adding. Each occurance of a 37 toggles between adding and subtracting. The 37's are NOT counted in the total at all. For example, {5, 30, 37, 2, 1, 37, 20} would result in 52. That result was obtained by: 5+30-2-1+20. Look at the test data for additional examples.


addSubtractArray37([1, 2, 3, 4, 5, 6]) → 21
addSubtractArray37([37, 1, 2, 3, 4, 5, 6]) → -21
addSubtractArray37([4, 10, 200, 37, 1, 12, 37, 50]) → 251

...Save, Compile, Run (ctrl-enter)

public int addSubtractArray37(int[] nums) { }

Editor font size %:
Shorter output


Forget It! -- delete my code for this problem

Progress graphs:
 Your progress graph for this problem
 Random user progress graph for this problem
 Random Epic Progress Graph

Java Help

Misc Code Practice

Difficulty: 290

Copyright Nick Parlante 2017 - privacy