0

Given the triangle of consecutive odd numbers:

1
3     5
7     9    11
13    15    17    19
21    23    25    27    29

// Calculate the row sums of this triangle from the row index (starting at index 1) e.g.:

rowSumOddNumbers(1); // 1
rowSumOddNumbers(2); // 3 + 5 = 8

I tried to solve this using for loops:

function rowSumOddNumbers(n){
  let result = [];

  // generate the arrays of odd numbers
  for(let i = 0; i < 30; i++){
    // generate sub arrays by using another for loop
    // and only pushing if the length is equal to current j
    let sub = [];
    for(let j = 1; j <= n; j++){
      // if length === j (from 1 - n) keep pushing
       if(sub[j - 1].length <= j){
         // and if i is odd
         if(i % 2 !== 0){
           // push the i to sub (per length)
           sub.push(i);
         }
       }
    }
    // push everything to the main array
    result.push(sub);
  }

  // return sum of n 
  return result[n + 1].reduce(function(total, item){
    return total += item;
  });
}

My code above is not working. Basically I was planning to 1st generate an array of odd numbers less than 30. Next I need to create a sub array base on the length of iteration (j) that would from 1 - n (passed). Then finally push it to the main array. And then use reduce to get the sum of all the values in that index + 1 (since the index starts at 1).

Any idea what am I missing and how to make this work?

2
  • Your best bet for understanding why this isn't working is to step through the code in a debugger, looking at the values of your variables and such. There's probably one built into your IDE (if not, consider looking at another IDE, like VS Code), there's one in Node (it uses Chrome as its UI), and of course in all major browsers. Commented Dec 2, 2017 at 12:28
  • As this is presumably an assignment, I won't post code, but: There's a dramatically simpler way to solve this problem; no need for arrays at all. The first number of a given row is n * (n - 1) + 1, and of course the row is n odd numbers long, so a short for loop or some further math does the job. Commented Dec 2, 2017 at 12:38

3 Answers 3

1

Most code problems involve some analysis first in order to spot patterns which you can then convert into code. Looking at the triangle, you'll see the sum of each row follows a pattern:

1: 1 === 1 ^ 3
2: 3 + 5 = 8 === 2 ^ 3
3: 7 + 9 + 11 = 27 === 3 ^ 3
... etc

So from the analysis above you can see that your code could probably be simplified slightly - I won't post an answer, but think about using Math.pow.

Sign up to request clarification or add additional context in comments.

Comments

0

No need for any loops.

function rowSumOddNumbers(n) {
    // how many numbers are there in the rows above n?
    // sum of arithmetic sequence...
    let numbers_before_n_count = (n - 1) * n / 2;

    let first_number_in_nth_row = numbers_before_n_count * 2 + 1;
    let last_number_in_nth_row = first_number_in_nth_row + 2 * (n - 1);

    // sum of arithmetic sequence again...
    return n * (first_number_in_nth_row + last_number_in_nth_row) / 2;
}

4 Comments

Posting complete rewrite code solutions to obvious homework (et. al.) assignments is not appropriate. (not my dv -- ah, it's been reversed)
@T.J.Crowder: So it's better to let OP use his "bad" code? (no offense, Tony Beatty, everyone was a beginner once) If I was solving some problem and needed help, I would definitely be more grateful for the most effective way how to solve it, so I could learn something new. (Not that I claim this is the most effective solution - feel free to suggest any enancements - but it's clearly better than using cycles.)
It's doing the work for the person instead of helping them do the work.
can you explain further your answer? give some analogy using the examples above passed in the function.
0

There is a simpler solution:

 function rowSumOddNumbers(n) {
  return Math.pow(n, 3);
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.