Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function subSum(arr, startIndex, endIndex) {
- if (!Array.isArray(arr)) {
- return NaN;
- }
- if (Number(startIndex) < 0) {
- startIndex = 0;
- }
- if (Number(endIndex) > arr.length - 1) {
- endIndex = Number(arr.length - 1);
- }
- const inputArr = arr.map(Number).slice(startIndex, endIndex + 1);
- const isNan = inputArr.some((el) => isNaN(el));
- if (!isNan) {
- let sum = 0;
- for (let i = 0; i < inputArr.length; i++) {
- sum += inputArr[i];
- }
- if (sum % 1 === 0) {
- return Number(sum);
- }
- return Number(sum.toFixed(1));
- }
- return NaN;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement