skip to Main Content

I’m trying to load a 6 line js file into the Node REPL using the .load function:

function reverse(string) { 
  return string.split("").reverse().join("");
}

function palindrome(string) {
  return string === reverse(string);
} 

The result is an infinite loop printing the first line of the file (see image). I have to kill the terminal to make it stop loading this one line of code. Is this a bug in Node, or am I missing something?

I’m using Ubuntu 22.04.01 LTS and Node.js v18.13.0.

en@dangerous:~/Documents/tutorial/js_tutorial$ node
Welcome to Node.js v18.13.0.
Type ".help" for more information.
> .load palindrome.js

Terminal Window

2

Answers


  1. I am also going through the Learn Enough to be Dangerous courses and I ran into exactly the same problem.

    As far as I can tell it has something to do with the node version since I was also in Node v18.13.0. I also tried 19.6.1 but the same thing happened.

    I decided to go backward and attempted to use 14.20.0 and low and behold I was able to run the palindrome.js file in a repl with node. I haven’t tested versions in between 14.20.0 and 18.13.0, so im not sure which version this started happening with.

    Update—
    I did some trial and error and I found that the issue starts specifically with 18.13.0. 18.12.1 runs the palindrome.js file just fine.

    For anyone curious this is what is in the palindrome.js file:

    // Reverses a string.
    function reverse(string){
        return Array.from(string).reverse().join("");
    }
    
    // Defines a Phrase Object.
    function Phrase(content){
        this.content = content;
    
        // Returns true for a palindrome, false otherwise.
        this.palindrome = function palindrome(){
            let processedContent = this.content.toLowerCase();
            return processedContent === reverse(processedContent);
        }
    
        // makes the phrase louder.
        this.louder = function(){
            return this.content.toUpperCase();
        }
    }
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search