'Why my javascript is showing reference error "Prompt is not defined"

// program to check if the string is palindrome or not

function checkPalindrome(str) {

    // find the length of a string
    const len = string.length;

    // loop through half of the string
    for (let i = 0; i < len / 2; i++) {

        // check if first and last string are same
        if (string[i] !== string[len - 1 - i]) {
            return 'It is not a palindrome';
        }
    }
    return 'It is a palindrome';
}

// take input
const string =prompt('Enter a string: ');

// call the function
const value = checkPalindrome(string);

console.log(value)

The Error is:

C:\Users\K.Santhosh kumar\Documents\my programs\.vscode\palindrome.js:20
const string =window.prompt('Enter a string: ');
              ^

ReferenceError: window is not defined
    at Object.<anonymous> (C:\Users\K.Santhosh kumar\Documents\my programs\.vscode\palindrome.js:20:15)
    at Module._compile (internal/modules/cjs/loader.js:1063:30)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:1092:10)
    at Module.load (internal/modules/cjs/loader.js:928:32)
    at Function.Module._load (internal/modules/cjs/loader.js:769:14)
    at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:72:12)
    at internal/main/run_main_module.js:17:47

And I also tried window.prompt



Solution 1:[1]

It seems you are running these codes in the Node environment. prompt is not a valid construct in the Node.js runtime (which assumedly is what you are running in through VSCode). It will only work in browser-based JavaScript engines.

There are libraries that offer similar functionality, but you would have to install via NPM and import them into your scripts.

Solution 2:[2]

prompt is not defined in your environment - likely because you are using Node and not a browser. Here is your code working in a browser.

To run this code in Node you could do something as per the code below - note it requires command prompt input. I also changed your checkPalindrom function to use str and not string which is a better approach:

function checkPalindrome(str) {

    // find the length of a string
    const len = str.length;

    // loop through half of the string
    for (let i = 0; i < len / 2; i++) {

        // check if first and last string are same
        if (str[i] !== str[len - 1 - i]) {
            return 'It is not a palindrome';
        }
    }
    return 'It is a palindrome';
}

const readline = require('readline').createInterface({
  input: process.stdin,
  output: process.stdout,
});

readline.question(`Enter a string: `, stringEntered => {
  readline.close();
  const value = checkPalindrome(stringEntered);
  console.log(value);
});

Solution 3:[3]

Use this line at the start of your file:

const prompt = require("prompt-sync")({sigint: true});

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Prime
Solution 2 TrevTheDev
Solution 3 Seph