1
\$\begingroup\$

This is my code to solve the 5th Adventjs challenge. In that link you can read the instructions.

How can I improve it? It's a bit messy... and I'm repeating some part of the code.

  • All must be in just one function
function cyberReindeer(road, time) {
  // Code here
  let theRoad = road;
  let roadNow = "";
  let greenLight = false;
  let roadArr = [];
  let stop = false;

  for (let i = 0; roadArr.length < time; i++) {
    if(greenLight !== true) {
      if (theRoad.charAt(i) !== "|" && stop !== true) {
        roadNow = theRoad.slice(0, i + 1)
          .replace("S", theRoad.charAt(i))
          .replace(/.$/,"S") 
          + theRoad.slice(i+1, theRoad.length);
        roadArr.push(roadNow);
      } else {
        while(roadArr.length < 5 && roadArr.length < time) {
          roadArr.push(roadNow);
          stop = true;
        }
        theRoad = theRoad.replace(/\|/g, '*');
        greenLight = true;
        roadNow = theRoad.slice(0, i + 1)
            .replace("S", ".")
            .replace(/.$/,"S") 
            + theRoad.slice(i+1, theRoad.length);
        if(roadArr.length < time) {
          roadArr.push(roadNow);
        }
      }
    } else {
      roadNow = theRoad.slice(0, i + 1)
            .replace("S", ".")
            .replace(/.$/,"S") 
            + theRoad.replace(/\|/g, '*')
            .slice(i+1, theRoad.length);
      roadArr.push(roadNow);
    }
  }
  return roadArr;
}
\$\endgroup\$
1

1 Answer 1

1
\$\begingroup\$

carefully read the requirements

The original problem asks us to

Create a function that simulates the sled's movement ...

Somehow you leapt from that to

All must be in just one function.

I see no prohibition on adding a helper function, if you find it convenient.


painting a sprite

Santa is a spritely old elf.

Three times you use an odd idiom for erasing his sled and then re-drawing it.

It would be simpler to just copy the original road terrain to the result each time (thereby "erasing" Sanata), followed by placing Santa at his new location between a pair of substrings.


velocity

Surprisingly, we're not modeling Santa's position with an x variable. The current solution resembles computation by cellular automata.

Having adopted that we might have a per time-step velocity delta_x, so the update for each cycle is x += delta_x. Setting that delta to zero would be a natural way to model Santa stopping briefly for a Red light.

consistent tests

I was a little surprised to see comparisons to 5 or to time like this: roadArr.length < time. It would have been more natural, at time-step i, to compare against the loop variable: i < time. Due to how the array grows, the effect is the same. Clarity could be improved.

separation of concerns

This simulation of the world has two things to manage each time-step:

  1. Stop lights ("barriers") sometimes switch from Red to Green
  2. Santa moves, subject to Red light restrictions

It might be worth breaking out a pair of helper functions for that.

This simple world seems very physics based to me -- Santa performs local sensing of an adjacent barrier. Yet we have a global comparison to 5 which alters Santa's behavior.

I was hoping to see simpler logic where we change the world by replacing all | bars with * stars when i == 5, and then we let Santa move when there's no | bar. The stop and greenLight booleans seem a bit much, it's more things to juggle in your head than are necessary.

\$\endgroup\$
3
  • \$\begingroup\$ Thank you. I'll take all into consideration and give it another try. When creating another function got this message: "Not allowed code outside the function. All your code MUST be inside cyberReindeer function" \$\endgroup\$ Dec 5, 2023 at 22:48
  • \$\begingroup\$ Wow, picky execution environment. Ok, one function it is. \$\endgroup\$
    – J_H
    Dec 5, 2023 at 22:52
  • \$\begingroup\$ sharing another solution clearly got some insights and found very useful resources lately. thanks. \$\endgroup\$ Dec 9, 2023 at 3:21

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.