SQL Server administration and T-SQL development, Web Programming with ASP.NET, HTML5 and Javascript, Windows Phone 8 app development, SAP Smartforms and ABAP Programming, Windows 7, Visual Studio and MS Office software Kodyaz Development Resources
Development resources, articles, tutorials, samples, codes and tools for .Net, SQL Server, Windows, Windows Phone, SAP and ABAP, like SAP UI5, Screen Personas, etc.

Number Sequence Puzzle Solution in Javascript

Morris Number Sequence puzzle is one of the popular mathematical puzzles that I want to solve using Javascript for developers to build programming algorithms. Javascript programmers will see that using Array object and Array methods with While and For loops, we can easily solve this problem also know as look-and-say sequence.

Let's first define the problem or question stated in the Morris number sequence puzzle.

Start with 1. As you see in below sequence image 1 is at the top. => 1

The second line can be read as follow: Previous line has one 1. => 1 1

Now, we can try to guess the third line: Previous line (which is line 2) has two 1. => 2 1

Fourth line summarizing the numbers in previous third line will be: One 2 and One 1. => 1 2 1 1

The lines and the content of each line is dependent on previous line and goes on like summarized above.

Google number sequence puzzle

What is important above is that: if same number is on the list side by side we count them. When a different number is in the order we write previously collected data and count the new number starting from 1.

Look and say sequence question is what is the content of the Nth line in this number sequence?

Javascript Code for Morris Number Sequence

In order to solve this mathematical puzzle (Morris Number Sequence), I preferred to code and simulate the case in Javascript.
I will try to explain the below Javascript code for programmers in order to read it easier.

Lines is the parameter to keep the number of lines to display. Change this if you wonder the sequence numbers for 100th line.

list is Javascript array object to keep number sequence on current line. I populate it with "1" at initialization using Javascript Array.Push() method.

Then a Javascript code block which runs once for each line within Javascript For loop.

var TopNumber = 1; // set the starting number, first number in sequence var Lines = 7; // number of lines to display

// initialize
var list = []; // new Array() for previous line;
var summary = []; // new Array() for calculation for next line;
list.push(TopNumber); // start sequence with 1

// start of each line for reading current and calculating for next line
for (var lineno = 1; lineno <= Lines; lineno++) {
 if (list.length == 1) {
  for (var i = 0; i < list.length; i++) {
  document.writeln("<br />");

 var current;
 var count;

 // start
 current = list[0]; // count this number
 count = 0;
 for (var i = 0; i < list.length; i++) {
  if (list[i] == current) {
   count++; // increase counter
  } else {
   summary.push(count); // save

   current = list[i]; // meet new number
   count = 1; // reset counter for new number

 for (var i = 0; i < summary.length; i++) {
 document.writeln("<br />");

 list.length = 0;
 list = list.concat(summary);
 summary.length = 0;

At each line's start, I reset parameter current which stores first number in the sequence. And set its count to 0.
Then I read each number in the sequence one by one, if number matches current I increase counter.

If a different number is recognized, I write the counter value and the counted item in summary Javascript array. I use this summary array to store calculations for next line in the sequence.

To store values in summary array, I use Javascript Array.Push() method.

When I reach to the end of that line, since I completed calculations at this point, I can securely get rid of the current line sequence which is stored in Javascript array named list.
Developers can see that I set the length of the list array to 0 in order to truncate Javascript array.
I found this method to delete all items in a Javascript array easier.

After I truncate javascript array list, I copy all items from summary array to list array using Javascript Array.Concat() method.
Array.Concat method adds the input array items to the end of the main array object.

Then I clear array items of summary Javascript array object.

Below is the output of above script with altered input lines parameter for 10 lines of numbers sequence puzzle.

I enjoy solving such well known puzzle in Javascript with simulating the case or problem by coding and hope you, programmers also like this solution of Morris number sequence mathematical puzzle.

If you are interested in Josephus problem and how to solve Josephus problem aka "the last person among 100 people with a sword around a circle", please check referenced Javascript tutorial.

Let's test sample Javascript solution to numbers sequence puzzle for 20th line.

numbers sequence puzzle solution in Javascript for 20 lines


Javascript Articles

Javascript Forums

Javascript Tutorials, Articles and Javascript Codes

Morris Number Sequence Puzzle Solution in Javascript
Javascript Array Puzzle: People with Sword Around a Circle
Sort Array in Random Order using Javascript Shuffle Function
Javascript MultiDimensional Array example codes
How many times a day a clock's hands overlap?
Javascript For Loop Sample Code
Use Javascript Replace Function Example to Replace Multi Space in a String with Single Space
Javascript String Trim with Javascript Trim Function Examples
Javascript String Split Function Example & Split Javascript Tutorial
Javascript Phone Format : Phone Number Format and Mask Telephone Numbers with Javascript
How to Enable / Disable Submit Button using Javascript According to any CheckBox is Checked on a WEB Page
Error Handling in JavaScript using Try...Catch Statement
How to display or unhide hidden table rows and hide or toggle a table row using Javascript functions
How to Replace All Occurrences of a String using Javascript Replace Function
How to autoformat and mask telephone numbers with Javascript
How to get key codes of characters using javascript code and key code list
Javascript Forums

Copyright © 2004 - 2017 Eralper YILMAZ. All rights reserved.
Community Server by Telligent Systems