lundi 8 mars 2021

Can I put a for statement inside of an if statement in Javascript? [duplicate]

In a program I am writing, I am supposed to be generating random integers between 0 and 10000.

One of my methods is to filter out certain values in that array, if they are generated. Once this occurs, it is supposed to print them in English.

I want my method to check if these values are generated and print them accordingly (see switch statement), but if not, I want to print a line that indicates that none of these values have been generated.

I feel as if I am overcomplicating this, but currently I have a nested if statement with a for loop. Is this possible? When I run my code with this method included, nothing compiles. The syntax is either probably incorrect, or I am trying to do something not tangible. Any and all help will be appreciated!

        
        if (this.values.includes(0 || 100 || 1000 || 10000)) {

            for (var x = 0; x < this.values.length; x++) {
                switch(this.values[x]) {
                case 0:
                    document.writeln(this.values[x] + ", the english word is ZERO");
                    break;
                case 100:
                    document.writeln(this.values[x] + ", the english word is One Hundred");
                    break;
                case 1000:
                    document.writeln(this.values[x] + ", the english word is One Thousand");
                    break;
                case 10000:
                    document.writeln(this.values[x] + ", the english word is Ten Thousand");
                    break;
                }
            }
        }
        
        else {
            document.writeln("No specified values could be filtered and printed because they were not present in the randomized array");
            break;
        }

    }

Aucun commentaire:

Enregistrer un commentaire