vendredi 3 décembre 2021

I am having issues with if-statements in Batch File [duplicate]

I am working on a script in batch for deleting files from all directories on my computer. I have the basics laid out, but I've run into an issue I have yet to encounter while working on Batch File code. I will put my code below, but what is happening is that when I run the code I cant make it past the first if-statement, I inserted pauses after each line so I could find out where the problem is and that's how I know this. What happens when I run the code is that it runs properly, It opens a terminal window, and it prompts the user for input, but immediately after I give the input something with the if-statement kills it. I've have done plenty of if-statements before in Batch File, but this is the first time I've ever encountered an error like this, some help would be greatly appreciated. Thanks in advance for all the help!

The Code


echo ~~~~~~~~~~~~~~
echo Program Delete
echo ~~~~~~~~~~~~~~
set /p choice=Please enter 1 to continue or 0 to return to menu: 
if %choice%==1 (
    pause
    Set /P inp=Please enter a file name: 
    pause
    for /f %%F in (C:\Program Files)(
        if "%%F" == "%inp%" (
        goto:remo
    )
)

(I haven't finished the code entirely yet, I'm just making sure the parts that I have made run properly.)

Comparison operators - if / else if statements

This exercise was already posted here a few years ago How to use comparison operators however the solution presented is not working for me.

The question is:

On the editor to your right you find a variable named charmanderLevel, to which a value between 1 and 100 will be assigned.

Using else if statements print to the console which evolution of Charmander corresponds to that experience level. Consider an else statement if the experience level ever go above 100 that should print 'Charizard is as good as it gets '.

Here's a chart with the evolution which corresponds to each level:

Charmander - 1 to 15 Charmeleon - 16 to 35 Charizard - 36 to 100"

The solution was

var charmanderLevel = Math.ceil(Math.random() * 100);

if (charmanderLevel >= 1 && charmanderLevel <= 15) {
  console.log('Charmander');
} else if (charmanderLevel >= 16 && charmanderLevel <= 35) {
  console.log('Charmeleon');
} else if (charmanderLevel >= 36 && charmanderLevel <= 100) {
  console.log('Charizard');
} else {
  console.log('Charizard is as good as it gets');
}

And I created literally the same

var charmanderLevel = Math.ceil(Math.random() * 100);

if (charmanderLevel >= 1 && charmanderLevel <= 15) {
  console.log("Charmander");
} else if (charmanderLevel >= 16 && charmanderLevel <= 35) {
  console.log("Charmeleon");
} else if (charmanderLevel >= 36 && charmanderLevel <= 100) {
  console.log("Charizard");
} else {
  console.log('Charizard is good as it gets');
}

But my console keeps saying

"Code is incorrect You should only print to the console the Charmander evolution level, nothing else".

They suggested that I opened a new question regarding this to ask to some of you some lights please, why it worked for some and mine is not.

Thanks

How do I loop through the program properly? [closed]

I just wanna ask something about my program. Here in my program, I want to be able to have a "return" option where you can go back to a previous menu. For example, choosing 1 to enter the formalscience menu then choosing mathematics and pressing 4 to return to formalscience menu, then returning back to the main menu. For some reasons, when I press 3 to return to the main menu from the formalscience menu, it loops to the formalscience menu but pressing 3 again will lead me to the main menu which is what I want. How do I fix this issue? I know I am doing something wrong with my loops so excuse me for that. I am a complete beginner to c++.

#include <iostream>
#include <string>

using namespace std;

class sciences
{
public:
    
    string formaloptions ()
    {
        int choice;
        
        cout << "You chose Formal science!\nFormal Science has two categories:\n";
        cout << "1. Mathematics\n2. Logic\n3. Back";
        cout << "\nWhat will you choose?\n";
        cin >> choice ; 
        
        switch (choice)
        {
            case 1:
            return "MATHEMATICS";
            break;
            case 2:
            return "LOGIC";
            break;
            case 3:
            return "BACK";
            break;
        }
    }
    
    
  };

  class subject 
  {
public:
    
    int mathematics()
    {
        int choice;
        
        cout << "You chose Mathematics!\n";
        cout << "Press 4 to return\n";
        cin >> choice ;
        
        switch (choice)
        {
            case 4:
                return 4;
                break;
        }
    }
    };

int main()
{
sciences sciencesobject;
subject subjectobject;

int choice;
int keytoformal;
int keytomath = 0;
int repeatertothemainmenu = 0;

do
{
cout << "BRANCHES OF SCIENCE\n\n";
cout << "Choose what do you want to tackle\n";
cout << "1. Formal\n2. Natural\n3. Humanistic\n";
cin >> choice;

switch (choice)
{
    case 1:
    keytoformal = 1;
    break;
}

while(keytoformal == 1)
{
if (sciencesobject.formaloptions() == "MATHEMATICS")
{
    keytomath = 1;
}
else if (sciencesobject.formaloptions() == "BACK")
{
    keytomath = 0;
    repeatertothemainmenu = 1;
    keytoformal = 0;
}
while (keytomath == 1)
{   
    switch (subjectobject.mathematics())
    {
        case 4:
        keytoformal = 1;
        keytomath = 0;
        break;
    }
}

}

 }while (repeatertothemainmenu == 1);
 }

Need the output of the inputs to be pushed into the

I'm trying to get the user inputs to be pushed out as a message into a <div> via the button press after confirming that all the inputs are filled and the correct password has been entered. That part works fine but my forEach is not working:

If the user has input a number value lower than 18, I want an if that types out via push "Your name is UName and you are Age years old, you are not of legal age", or else the same result as above but ending with "...you are of legal age".

Any idea how I should go forth?

let nameElement = document.getElementById("UNameInput");
let faultElement = document.getElementById("errorOutput");
let ageElement = document.getElementById("AgeInput");
let clickElement = document.getElementById("button");
let passwordElement = document.getElementById("PasswordInput");
clickElement.addEventListener("click", function(e) {

  let feedback = [];
  if (UNameInput.value === '' || UNameInput.value === null) {
    feedback.push('Name input missing')
  }

  if (AgeInput.value === '' || AgeInput.value === null) {
    feedback.push('Age input missing')
  } else if (PasswordInput.value !== 'IHM') {
    feedback.push('Password is not valid')
  } else {
    feedback.forEach((item, i) => {
      if (item.AgeInput < 18) {
        feedback.push("You are not of not legal age")
      } else {
        feedback.push(" You are of legal age ")
      }
    });
  }
  if (feedback.length > 0) {
    e.preventDefault()
    faultElement.innerText = feedback.join(", ")
  }
});
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <script src="JSny.js" defer></script>
</head>

<body>
  <div id="errorOutput"></div>
  <p id="output"></p>
  <div>
    <label for="UNameInput">Name Input</label>
    <input id="UNameInput" name="name" type="text">
  </div>
  <div>
    <label for="AgeInput">Age Input</label>
    <input id="AgeInput" name="age" type="number">
  </div>
  <div>
    <label for="PasswordInput">Password Input</label>
    <input id="PasswordInput" name="password" type="password">
  </div>
  <button id="button">Submit</button>
</body>
</html>

Checked multiple times and also typecasted explicitly to avoid errors but still this program showing wrong results. What to do?

Trying to implement default constructor and some basic functions. Although the logic behind this program is clear but I don't know why the output is wrong every time. To avoid any kind of issues I typecasted explicitly but still seems like error hasn't resolved.

import java.util.*;

class Facebook {
  Facebook() {
    System.out.printf("Welcome to Facebook.\n");
  }

  boolean Login(String uName, String pWord) {
    if(uName == "knownassurajit" && pWord == "password")
      return true;
    else
      return false;
  }

  public static void main(String[] args) {
    Facebook user = new Facebook();
    Scanner scanner = new Scanner(System.in);

    System.out.printf("Username: ");
    String username = scanner.next();
    System.out.printf("Password: ");
    String password = scanner.next();

    //System.out.println(username.length() + " " + password.length());

    if(user.Login((String) username, (String) password)) {
      System.out.println("Successfully logged in.");
    }
    else {
      System.out.println("Username or Password incorrect.");
    }
  }
}

This code will work perfectly with equals() function.

import java.util.*;

class Facebook {
  Facebook() {
    System.out.printf("Welcome to Facebook.\n");
  }

  boolean Login(String uName, String pWord) {
    if(uName.equals("user") && (pWord.equals("p@$$w0rd")))
      return true;
    else
      return false;
  }

  public static void main(String[] args) {
    Facebook user = new Facebook();
    Scanner scanner = new Scanner(System.in);

    System.out.printf("Username: ");
    String username = scanner.next();
    System.out.printf("Password: ");
    String password = scanner.next();

    if(user.Login((String) username, (String) password)) {
      System.out.println("Successfully logged in.");
    }
    else {
      System.out.println("Username or Password incorrect.");
    }
  }
}

jeudi 2 décembre 2021

I want to create IF ELSE statement for NULL formitems on Blazor

I wanted to create a handler that when the submit button is trigger it should add the record on the database but when the fields are Null or Empty it should throw an error. In my case it always passes. Codes are below:

 private async Task OnSubmitHandler(EditContext editContext)
    {
        AssigneeDto assigneeDto = editContext.Model as AssigneeDto;

        if (assigneeDto != null)
        {
            await _assigneeClient.CreateAssignee(sm.id, assigneeDto);
            NotificationReference.Show(new NotificationModel()
            {
                ThemeColor = ThemeColors.Success,
                Text = "Record Added Successfully!",
            });
            FormData = new AssigneeDto();
            CurrentForm = new EditContext(FormData);
            CurrentForm.AddDataAnnotationsValidation();
        }
        else
        {
            NotificationReference.Show(new NotificationModel()
            {
                ThemeColor = ThemeColors.Error,
                Text = "Record Could Not Be Added!",
            });
        }
    }

If statements not moving to the else statement in a game of chance in JavaScript

Hello I am writing pretty much a game of chance with JavaScript and for some reason even when the right answer is given it displays that you lost. I have tried to change the if conditions but am not well versed enough to figure it out. This is my first time asking a question on here if you need anymore info I will supply what I know and if this has been answered already I could not find it or did not recognize it as the answer as like I said I am still pretty new at coding in general.

This is the HTML

 <h3>CAN YOU GUESS MY NUMBER!!</h3>
    <h4>You get two guess and my number is random every time you try. Goodluck!</h4>
    <input type="text" onkeyup="guessOneCheck(this)" placeholder="enter your guess 1-10" id="guessOne" maxlength="2">
    <input type="text" onkeyup="guessTwoCheck(this)" placeholder="enter your guess 1-10" id="guessTwo" maxlength="2">
    <button id="submit__Guesses" onclick="bothWrong()">SUBMIT GUESSES</button>
    <div id="guessAnswer"></div>
 <script src="main.js"></script>

This is the javascript

let guessOne = ''
let guessTwo = ''

const guessOneCheck = (g1) => {
  guessOne = g1.value
  console.log(g1.value)
}

const guessTwoCheck = (g2) => {
  guessTwo = g2.value
  console.log(g2.value)
}

const bothWrong = () =>{
  let randomNum = Math.floor((Math.random() *10) + 1);
  console.log(randomNum)

  if(guessOne !== randomNum && guessTwo !== randomNum){
    document.getElementById('guessAnswer').innerHTML = 'HaHa you lose better luck next time!! My number was' + ' ' + randomNum;
  } else if(guessOne == randomNum || guessTwo == randomNum){
    document.getElementById('guessAnswer').innerHTML = 'What! No you cheated, or omg are you a psychic!!! My number was' + ' ' + randomNum;
  }
}