# The data structure

How to implement or finding the longest common subsequence using dynamic programming with the proper way to testing the code?

Hey @Dode83 !

Have you tried googling this question?

There are a ton of videos and articles for this problem.

Hope that helps!

I tried and I did that code but still get no data in console.

It would help the community if you shared your code.

``````function lcs(X, Y) {

var m = X.length;

var  n = Y.length;

l = [];

i, j, a, b;

for (i = 0; i <= m; ++i) {

l[i] = [];

for (j = 0; j <= n; ++j) {

l[i][j] = 0;

}

}

for (i = 0; i <= m; i++) {

for (j = 0; j <= n; j++) {

if (i == 0 || j == 0) {

l[i][j] = 0;

} else if (X[i - 1] == Y[j - 1]) {

l[i][j] = l[i- 1][j - 1] + 1;

} else {

a = l[i - 1][j];

b = l[i][j - 1];

l[i][j] = (a > b) ? a : b; //max(a,b)

}

}

}

return l[m][n];

}
``````

I’ve edited your post for readability. When you enter a code block into a forum post, please precede it with a separate line of three backticks and follow it with a separate line of three backticks to make it easier to read.

You can also use the “preformatted text” tool in the editor (`</>`) to add backticks around text.

See this post to find the backtick on your keyboard.
Note: Backticks (`) are not single quotes (’).

You have some errors in your code which is why you are not able to see the results in the console.

`````` //use let instead
var m = X.length;

var  n = Y.length;
``````
``````  //not defined
l = [];

//not defined
i, j, a, b;
``````

Also, I am personally not a fan of 1 letter variable names because it get a little confusing on what each variable is supposed to be doing.

I haven’t done this rosetta code challenge yet but I would go through and throw in some console.logs and test out what these for loops are supposed to be doing and debug your code from there.

Good luck!

1 Like

You really like these math problems. What have you tried so far?

1 Like

I tried that a lot but it is really never works with me.
Of course, I do like this stuff so much. i did with a recursive function.

@Dode83 If you want help with your code you should post it.

It might also help to checkout something like a code kata for the problem. The problem might be explained a little better and it gives you some predefined tests. However, it might also have extra requirements or a different setup.

The following iterative sequence is defined for the set of positive integers:

n → n/2 (n is even)
n → 3n + 1 (n is odd)

Using the rule above and starting with 13, we generate the following sequence:

13 → 40 → 20 → 10 → 5 → 16 → 8 → 4 → 2 → 1

It can be seen that this sequence (starting at 13 and finishing at 1) contains 10 terms. Although it has not been proved yet (Collatz Problem), it is thought that all starting numbers finish at 1.

Which starting number, under one million, produces the longest chain?

NOTE: Once the chain starts the terms are allowed to go above one million.

That is not code, you just posted the problem description.

What have you tried?
Do you understand what is being asked for?

If you do not fundamentally understand what is being asked for, check out the code kata. It explains it better.

I totally understand the question and this is my code but the problem is… this doesn’t work when I execute it as a console.

``````function length(n) {

for (var c = 1; n > 1; c++) {
if (n % 2 === 0) {
n/= 2;
} else {
n = 3 * n + 1;
}
}
return c;
}

function arr(n) {
var max = 0;
max_i = 0;

for (var i = 1; i < n; i++) {
var c = length(i);

if (c > max) {
max = c;
max_i = i;
}
}
return max_i;
}

var H = {1: 1};

function length(n) {

if (H[n] === undefined) {
H[n] = 1 + length(n % 2 ? 3 * n + 1 : n / 2);
}
return H[n];
}
console.log(arr);
``````

you call variable `arr` and it doesn’t exist, but you have a function `arr` and you can use with `arr(0)`.

Thank you so much I got it … 