Andrew Sladky
Andrew Sladky

Reputation: 1

How do I get my if/else statement to run a bunch of functions if my IF statement is false?

So what I am trying to do is make a website that can convert the percentages of each letters average frequency in the english language to the amount of times they should appear in a given message length, and here is the part of my code that isn't working.

var Messagelength = prompt("What is the length of a single passage from the broken-up cryptogram? Write down as the numbers appear, or else you will    need to refresh this page and try again.");
if (0 >= Messagelength)(true){
    confirm("No negative numbers or 0 are accepted")};
if (0 >= Messagelength)(false){
var MessagelengthA = function(number) {
    var PercentageA = number * 0.08167;
    confirm(PercentageA);
};
};

What I want it to do is run the rest of my script like normal, but only if the number that is given from a prompt asked in the unseen part of the code is greater than 0. So after a few attempts with what people have told me to do, I've ended up deciding this is my best stepping stone towards my goal. The problem remains though, it doesn't work. If I were to remove the second if statement, the first plays regardless, and if I remove the first one or just leave the second if statement, then nothing happens, not even the initial prompt deciding what the value of Messagelength is. So to recap, my question is, how do I get the page to run a script of 26 different functions, but only if the number the user puts in the prompt is greater than 0, and NOT run those 26 functions if it is NOT greater than 0?

EDIT: I was able to find my error and correct it. I had a semicolon at the end of my if statement, and it shouldn't have been there. The script is finally working the way I had wanted it too. Thanks!

Upvotes: 0

Views: 46

Answers (2)

Barmar
Barmar

Reputation: 781814

You said it in your question title. This is an if/else statement, so put the rest of the code in the else block.

var MessagelengthA = function(number) {
    var PercentageA = number * 0.08167;
    confirm(PercentageA);
};
if (MessageLength <= 0) {
    alert("No negative numbers or 0 are accepted");
} else {
    MessageLengthA(MessageLength);
    // rest of code here        
}

Upvotes: 2

Mike Cluck
Mike Cluck

Reputation: 32511

Remove continue. That's for restarting loop bodies.

An if statement merely provides a way of running code if a given condition is true. If it is, that block of code run. Regardless of if it does or not, the following block runs.

if (true) {
  console.log('The condition is true so I run');
}
console.log('I run just because I come next in the script');
if (false) {
  console.log('I will never run');
}
console.log('Again, I will run');

So your code will automatically run after your if statement. What it sounds like you want is a way to prevent the following code to run under some circumstances. The simplest way is to use an else.

if (true) {
  console.log('true condition, I run');
} else {
  console.log('I do not since the preceding `if` block did');
}

if (false) {
  console.log('I will not run');
} else {
  console.log('since the condition failed, I will run');
}

Upvotes: 1

Related Questions