# Data Structure

#1

<Below this line, add a link to the EXACT exercise that you are stuck at.>

<Below this line, in what way does your code behave incorrectly? Include ALL error messages.>
Hi, I was stuck on two problems on my codes, here are the two problems:
(a) Not sure where I did wrong on my codes, cause I can’t get my sum function run effectively

(b)Well, can anyone tell me what I should do to get the value in “listofNumbers” array backward as the example said, for example “range(5,2,-1)”, i’d like to get the array like this [5,4,3,2,]

Really appreciated!!!

instructions:
1.Write a range function that takes two arguments, start and end, and returns an array containing all the numbers from start up to (and including) end.

2.Next, write a sum function that takes an array of numbers and returns the sum of these numbers. Run the previous program and see whether it does indeed return 55.

3.As a bonus assignment, modify your range function to take an optional third argument that indicates the “step” value used to build up the array. If no step is given, the array elements go up by increments of one, corresponding to the old behavior. The function call range(1, 10, 2) should return [1, 3, 5, 7, 9]. Make sure it also works with negative step values so that range(5, 2, -1) produces [5, 4, 3, 2].

``` var listofNumbers = [0,1,2,3,4,5,6,7,8,9,10];

var total = 0;
function range(start, end, i) {
function sum (numbers) {
for(x=start;x<end;x++){
total = total + listofNumbers.index(x);
}
}
if (i === undefined )
return listofNumbers.slice(start, end+1);
return sum ();
}

console.log(range(1, 10));
// → [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
console.log(range(5, 2, -1));
// → [5, 4, 3, 2]
console.log(sum(range(1, 10)));
// → 55

``<do not remove the three backticks above>``

#2

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.