Markov test : "rules undefined"

Tell us what’s happening:

Hi !

I encountered a problem with the test “Markov”. Why have I the output “rules is not defined”?
I don’t understand what I’m doing wrong.

Thanks in advance

Your code so far

function markov(rules, test) {
const regex = /#.*/gm;
    let line = 0, pattern, replacement, terminating = false;

    while (line < rules.length) {

      if (regex.test(rules[line])) {

      [pattern, replacement] = rules[line].split(' -> ').map(x => x.trim());

      if (replacement.charAt(0) == '.') {
        terminating = true;
        replacement = replacement.substr(1);

      test = test.replace(pattern, replacement);
      if (terminating) break;
    return test;

Your browser information:

User Agent is: Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:70.0) Gecko/20100101 Firefox/70.0.

Challenge: Execute a Markov algorithm

Link to the challenge:

Welcome to the forum!

I haven’t gotten into these challenges yet but I used your code to practice debugging. This one had me baffled even after manually defining rules & tests, so I had to look it up:

Add this to the end of your code and it will pass a few:

// tail:
let rules=[["A -> apple","B -> bag","S -> shop","T -> the","the shop -> my brother","a never used -> .terminating rule"],
			["A -> apple","B -> bag","S -> .shop","T -> the","the shop -> my brother","a never used -> .terminating rule"],
			["A -> apple","WWWW -> with","Bgage -> ->.*","B -> bag","->.* -> money","W -> WW","S -> .shop","T -> the","the shop -> my brother","a never used -> .terminating rule"],
			["_+1 -> _1+","1+1 -> 11+","1! -> !1",",! -> !+","_! -> _","1*1 -> x,@y","1x -> xX","X, -> 1,1","X1 -> 1X","_x -> _X",",x -> ,X","y1 -> 1y","y_ -> _","1@1 -> x,@y","1@_ -> @_",",@_ -> !_","++ -> +","_1 -> 1","1+_ -> 1","_+_ -> "],
			["A0 -> 1B","0A1 -> C01","1A1 -> C11","0B0 -> A01","1B0 -> A11","B1 -> 1B","0C0 -> B01","1C0 -> B11","0C1 -> H01","1C1 -> H11"]];
let tests=["I bought a B of As from T S.",
			"I bought a B of As from T S.",
			"I bought a B of As W my Bgage from T S.",
let outputs=["I bought a bag of apples from my brother.",
			"I bought a bag of apples from T shop.",
			"I bought a bag of apples with my money from T shop.",

I haven’t gotten to this point yet, so not sure if intentional or not? @RandellDawson will know.

Git Hub for this challenge:

Rosetta Code is a complicated topic in general. My suggestion, just ignore Rosetta Code! It wasn’t created for problem solving challenges but to demonstrating and mostly archiving different programming languages by presenting solutions to various problems with these languages.

You can think of it as an archive of all natural languages that stores the same stories written in different languages. These stories could be inaccurate and contain mistakes - that’s ok, because the goal is to showcase language and not the story. Same here - MOST of the problems on Rosetta Code are critically inaccurate and ambiguous, and people should stop turning them into challenges!!! :man_facepalming:

1 Like

Great! Thank you so much! I didn’t know that i have to put manually the tests and outputs. With some debugging, i passed the test. :slightly_smiling_face:

Have a nice day!