Help With Summing Odd Numbers

Hi guys, I’m having trouble figuring this out. I got the JS certification yesterday, but I’m still a bit uneasy I really know what I’m doing. I ran across a Code Wars algorithm challenge and was doing really good up to this point. Can anyone help me out?

The problem is in the conditional if (rangeDiff > 2) statement.

function rowSumOddNumbers(n) {
	let firstOddNum = ((n * (n - 1)) + 1);
	console.log(firstOddNum);				// 1, 3, 7, 13, 21

	let lastOddNum = (firstOddNum + (2 * (n - 1)));
	console.log(lastOddNum);				// 1, 5, 11, 19, 29

	let rangeDiff = lastOddNum - firstOddNum;
	console.log(rangeDiff); 				// 0, 2, 4, 6, 8

	let rangeArr = [];

	if (rangeDiff > 2){
		for (let i = 0; i < lastOddNum && i > firstOddNum; i + 2) {
			return rangeArr.push((i + (rangeDiff / 2)).toString);
		}
		console.log(rangeArr);
		//Add to array firstOddNum + (rangeDiff / 2)
		//are there any more numbers? for loop??
		//Add all values of the array of odd numbers here.
	} else if (rangeDiff < 2) {
		if (rangeDiff === 0) {
			return firstOddNum;
		} else {
			return firstOddNum + lastOddNum;
		}
	} else {
		return undefined;
	}
}

				//		1st Odd		next num
rowSumOddNumbers(1);	//1 		(firstOddNum + (2 * (n - 1)))	(n = items in row)
rowSumOddNumbers(2);	//3 										(n = 2)
rowSumOddNumbers(3);	//7 										(n = 3)
rowSumOddNumbers(4);	//13										(n = 4)
rowSumOddNumbers(5);	//21										(n = 5)

Output

1
1 
0 
3 
5 
2 
7 
11
4 
[]
13
19
6
[]
21
29
8
[]

Additional info:

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

You’ve literally almost solved in in the very first line: the rest of the code is not necessary. Once you know how to figure out mathematically (rather than looping) what the first number in the row is you’ve already done most of the work. Literally this: (n * (n - 1)) + 1 – you can add maybe a few more digits and operators and you’re done, if you want.

Look at the rows of numbers: you can make each individual row be all the same number by subtracting from the rightmost numbers and adding to the leftmost numbers. Then, once you know what that number was, ie what to add to firstOddNum, you could just multiply by the row number (as row 3 has 3 digits, they’re all the same, row 121 has 121 digits, they’re all the same).

spoilered further hint:

On the rows with an odd number of digits this is always there, it’s middle value, so what would you add to 21 to get that middle value? And how does that middle value relate to the number of the row 21 belongs to?

And I’ve tried to only go from that first line of code you had. As a second hint:

it’s simpler by far if you look at what the relationship between the row number and what the number in the middle of that row is. If you can see the pattern, you don’t need to know what the first number in the row is. Or the last number. Or any number, apart from the row number.

1 Like

There is another way to approach this problem which I think is very simple once you realize what each row’s sum has in common. Let’s look at the first 5 rows’ sums:

sum of row #1 = 1
sum of row #2 = 8
sum of row #3 = 27
sum of row #4 = 64
sum of row #5 = 125

It turns out the row number is the cube root of the row’s sum.

Spoiler:

You can see this fact by breaking down the row sum as a product of the row number multiplied by itself 3 times.

1 is the product of 1 * 1 * 1
8 is the product of 2 * 2 * 2
27 is the product of 3 * 3 * 3
64 is the product of 4 * 4 * 4
125 is the product of 5 * 5 * 5

The sum of any row of odd numbers is just the row number cubed.

1 Like

Thank you both very much. The cubic solution was the best, but I do understand the other one. Guess I learned more than I though with FCC. Thanks again, guys!

(Mine was also the cubic solution btw, it’s literally the exact same - middle of row 1 is 1 and row length is 1, middle of row 2 is 4 and row length is 2, middle of row 3 is 9 and row length is 3)

1 Like