Home > Back-end >  JavaScript script creating a list of three numbers
JavaScript script creating a list of three numbers

Time:12-28

I’m new here, and so on coding.

I friend of mine suggests me to learn JavaScript and Python, because I love riddles that I can’t solve (so this languages could help me).

Let me explain: I want to create a JS script starting from this real-life problem.

I have a padlock which have a code of three numbers to be unlocked (you have to turn upside down these numbers to obtain the “Open sesame”), the code goes to 000 to 999, obviously.

I need to create a script that lists all the possible numbers and, at the end, also tell me how many different numbers I have (I suppose 1000 if my math isn’t bad as my english).

I started the learning path, but I’m not able to create this script.

I need to check all the different combinations that i have done for unlock the padlock

Can someone help me?

Thank you so much

ps: it could be nice also the same script in bash, which it's more familiar to me

x 0stone0: I have non familarity with JavaScript, I've only done an online course, so I made no attempt, just asking. For bash, I found right here a "skeleton" of permutation script like this:

for X in {a..z}{a..z}{0..9}{0..9}{0..9}
    do echo $X;
done

but I really don't know ho to edit it, cause I don't know hot to save the output of three numbers YYY from 0 to 9

CodePudding user response:

Javascript

let i = 0; 
while (i <= 999) { 
  console.log(String(i).padStart(3, '0')); 
  i  ;
}


Bash

for X in {0..9}{0..9}{0..9}; do 
    echo $X; 
done
Try it online!

CodePudding user response:

Using js, you can do:

let count = 0;
for (let i = 0; i <= 999; i  ) {
    count    // count   is the same as count = count   1, count is used to count the number of times the loop has run

    if (i < 10) {             // if i is less than 10 add two zero before it, for it to look like (009)
        console.log('00'   i);
    } else if (i < 100) {     // if i is less than 100 add one zero before it, for it to look like (099)
        console.log('0'   i);
    } else if (i < 1000) {    // if i is less than 1000 add nothing before it, for it to look like (999)
        console.log(i);
    } else {
        console.log(i);
    }
}

// then we console.log() the count variable
console.log(`There is ${count} possibilities`);

The program is made to show 3 digits, so if it's 9, it will show 009 and same for 99 => 099

  • Related