How can I get my code to accept each user input for the same prompt as separate variables?


coding beginner here! Recently, I’ve been trying to make a simple shift cipher using Node.js. The first user-submitted number is how many numbers each digit shifts, the second is the number of digits, then how many times the program asks for a new digit is based on the number the user inputs in the second prompt. I need every digit to be stored as it’s own variable so that I can can shift each digit by the key and output it to the console.

Here’s what my code looks like so far (omitting set-up that isn’t relevant here):

key = prompt("Enter encryption key: ");
while (key < 0) {
    key = prompt("Please enter a positive key: ");
}

digits = prompt("Number of digits: ");
while (digits <= 0) {
    digits = prompt("Please enter a digit 1-9: ");
}
while (digits > 9) {
    digits = prompt("Please enter a digit 1-9: ");
}

while (i <= digits) {
    dig = prompt("Enter digit " + i + ": ");
    i++
}

And here’s an example of the terminal output so far:

Enter encryption key: 4
Number of digits: 3
Enter digit 1: 8
Enter digit 2: 2
Enter digit 3: 6

I’m pretty sure that I did something wrong in the last block of code, but I don’t know how to fix it. I’ve been trying all day, anyone know what’s wrong with this? Is it even possible to do this in JavaScript? Thanks in advance!

Source: JavaSript – Stack Overflow

November 6, 2021
Category : News
Tags: javascript | node.js | user-input

Leave a Reply

Your email address will not be published. Required fields are marked *

Sitemap | Terms | Privacy | Cookies | Advertising

Senior Software Developer

Creator of @LzoMedia I am a backend software developer based in London who likes beautiful code and has an adherence to standards & love's open-source.