# HailStoneSequence Solution

What is your hint or solution suggestion?

``````function hailstoneSequence() {
var res = [], n =27, c1 = [],  c2 = [351,77031];
length... Please don't make the poor machine do it again... */

c1.push(n);
/*While Res Loop Collatz 1*/

while(n != 1){
n = (n%2== 0) ? (n/2): (3*n + 1);
c1.push(n);

}//End of c1

var newC1 = [c1[0], c1[1], c1[2], c1[3],
c1[c1.length-4], c1[c1.length-3], c1[c1.length-2],
c1[c1.length-1] ];

c1 = newC1;

console.log(`The final c1 matrix before c2 data is \${c1}.\n`);

res.push(c1);
res.push(c2);

console.log(`Final Collatz result matrix is \${res}.\n`);
return res;
} /*My 1st Ancient Egyptian Spoiler Tag... Sorry*/
``````

Challenge: Hailstone sequence

Hey @mlpfanatic903!

So I was corrected that for the guide submissions to use the `[details][/details]` tags.

So that was my bad. Sorry.

You are doing great with these rosetta problems. Keep up the great work. I just finished the 100 doors challenge.

Happy coding!

Thanksâ€¦ Same to you. Iâ€™ve explored the ideas behind the 100 doors challenge, and the map versions seem to run the best for whatever reasonâ€¦

I have a couple of issues with that solution, it uses old (var) syntax, and it doesnâ€™t really follow the instructions (which suggest to write a helper function to get the hailStone sequence for any given number). This function could then be re-used to calculate the solution for the second task, instead of hardcoding it.

``````function hailstoneSequence() {
const res = [];

/* 1.: Create a routine to generate the hailstone sequence for a number */
const hailStones = n => {
let result = [n];
while (n > 1){
n = n%2 === 0 ? n/2 : 3*n+1;
result.push(n)
};
return result;
}

/* 2.: Hailstone sequence for n=27 */
let n = 27;
const seq1 = hailStones(n);
const res1 = [...seq1.slice(0,4), ...seq1.slice(-4)];
res.push(res1);

/* 3.: Hailstone sequence for n=77031 */
n = 77031;
const seq2 = hailStones(n);
const res2 = [seq2.length, n];
res.push(res2);

return res;
}
``````

So we can agree on the method of implementation, but not on the intrinsic code that does the same functionâ€¦ Interesting Qâ€¦ To each their own, I guessâ€¦
Nice touch with the .slice() method btw.

I just thought that when someone looks up the hints for the second task, and all he finds is a hardcoded array `[351,77031]`, it could be a little disappointing. They may wonder why the challenge includes that second task at all.

Thank you for your guide post contribution. I have taken your suggestions and included the combination in the guide post.

We look forward to your further contribution.

1 Like