Why wont this IF statement work in Unity? - if-statement

Okay I have a problem. I am using an IF statement. It seems to not run like it should. It works fine without the || condition, but with the || it seems to just pull the text under and not run the ELSE...
var fateText : UI.Text;
var inputText : UI.Text;
function fateBall () {
if(inputText.text == "illuminati"||"Illuminati"){
fateText.text = "We run the entire world. Join us!";
}else{
var myRandomString : String = RandomWordString(1);
fateText.text = myRandomString;
}
}
If i remove the ||"Illuminati" it works great... but like this it assigns fateText.text to "We run the entire world." and not the myRandomString
EDIT REPORT: Okay, the .ToLower() worked great now I am running into a problem where when I add multiple IFs it just bypasses the IFs and just runs the ELSE... any ideas?
function fateBall () {
if(inputText.text.ToLower() == "illuminati"){
fateText.text = "We run the entire world. Join us!";}
if(inputText.text.ToLower() == "satan"){
fateText.text = "Lucifer is the Light Bringer. He leads us against God!";}
if(inputText.text.ToLower() == "devil"){
fateText.text = "Lucifer is the Light Bringer. He leads us against God!";}
if(inputText.text.ToLower() == "lucifer"){
fateText.text = "Lucifer is the Light Bringer. He leads us against God!";}
else{
var myRandomString : String = RandomWordString(1);
fateText.text = myRandomString;
}
}

The condition must be :
if(inputText.text == "illuminati" || inputText.text == "Illuminati")
Otherwise, you can set to lower all the text so as to be case-independant :
if(inputText.text.ToLower() == "illuminati")
An even better way to compare strings is to use the Equals function (C# only though)
if( String.Equals( inputText.text, "illuminati", System.StringComparison.InvariantCultureIgnoreCase)
EDIT further to your EDIT:
string lowerText = inputText.text.ToLower() ;
if(lowerText == "illuminati")
{
fateText.text = "We run the entire world. Join us!";
}
else if(lowerText == "satan")
{
fateText.text = "Lucifer is the Light Bringer. He leads us against God!";
}
else if(lowerText == "devil")
{
fateText.text = "Lucifer is the Light Bringer. He leads us against God!";
}
else if(lowerText == "lucifer")
{
fateText.text = "Lucifer is the Light Bringer. He leads us against God!";
}
else
{
var myRandomString : String = RandomWordString(1);
fateText.text = myRandomString;
}

Try changing the condition from:
inputText.text == "illuminati"||"Illuminati"
To this:
inputText.text == "illuminati"|| inputText.text =="Illuminati"
I don't think you can chain conditions like that.
In addition you may want to just use a lowercase function to simplify your condition. So I believe you can do the following instead.
if(inputText.text.ToLower()=="illuminati")

If you want to check multiple condidtions in one if statement do it like this:
if (var1 == condition1 || var2 == condition2){}
this will check if either one return true, but you can also check if multiple statements are true by using:
if (var1 == condition1 && var2 == condition2){}

Related

the var rate of 1000 can't determine the string?

When I input the BSIT in the prompt, the rate is always 1200 even though the string is correct
<html> <HEAD>
<TITLE> Simple Enrollment Computation </TITLE>
<SCRIPT language="JavaScript">
<!--hide
alert('Hi, Welcome!');
var yourname = prompt('Please enter your name.');
var course = prompt('Enter your Course');
var units= prompt('Enter number of units:');
if ( (course = 'BSIT') || (course = 'bsit') )
{
var rate = 1000
}
else ( (course == ' ') || (course == null) )
{
var rate = 1200
}
var total= rate * units
You have a bug in your code that throws the path of execution off.
An else clause never takes a condition - if the condition on the if evaluates to false, the else is executed - and in your code snippet that means that the test behind the else is evaluated. However, since the result is never assigned to anything, it is lost. The block right behind that is executed no matter what, thereby unconditionally setting rate to a value of 1200.
Original statement, with annotations and slightly reformatted:
if ( (course = 'BSIT') || (course = 'bsit') )
{
// Will be executed when course == 'BSIT' or 'bsit'
var rate = 1000
}
// If course == ' ' or null, else is invoked, and since the condition
// is directly following the else, that is evaluated.
else
( (course == ' ') || (course == null) )
// The following block is NOT attached to the else and so gets executed
// no matter what!
{
var rate = 1200
}
Your best bet would be to either put an if right behind the else (this way the condition has an effect, and the previously dangling block becomes part of the second if statement) or change it into a switch statement.
Modified if statement:
var rate; // One declaration is entirely sufficient.
if((course == 'BSIT') || (course == 'bsit'))
{
rate = 1000;
}
else
if((course == ' ') || (course == null))
{
rate = 1200;
}
switch statement:
var rate;
switch(course)
{
case 'BSIT':
case 'bsit':
rate = 1000;
break;
case ' ':
case null:
rate = 1200;
break;
}
Each variant is going to fix that problem, however, you are going to wind up with an undefined value in rate if anything else than the expected values is entered. You should also account for that and assign a default value before doing any checks.

why are my if and else if statements not running?

same as title.
const inputs =document.querySelectorAll('input');
inputs.forEach(function(_inputs){
_inputs.readOnly = !_inputs.readOnly;
if(_inputs.readyOnly == false){
_inputs.classList.remove("readOnlyItem")
_inputs.classList.add("notReadOnly")
}else if(_inputs.readyOnly == true){
_inputs.classList.add("readOnlyItem")
_inputs.classList.remove("notReadOnly")
}
});
As far as I can tell every thing seems right and should be working but the if statments never return true for some reason.

Hello everyone, please help me check this IF statement in Google app script

I want to make a code to assign logic input for my sheet. I use IF to make it. My code ran successfully but the logic didn't work. I have checked it many times, but I couldn't find something wrong. Can you help me with this? I'm stuck. Please review my example sheet and my script for more information. Thank you! https://docs.google.com/spreadsheets/d/1eV2SZ45Gs6jISgh_p6RIx-rfOGlHUM6vF114Mgf6c58/edit#gid=0
function logic(){
var ss = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet();
var activeCell = ss.getActiveCell();
if (activeCell.getColumn() == 1 && activeCell.getRow() > 1 && ss.getSheetName() == "mama" && activeCell.getValue() == "Yes") {
activeCell.offset(0,1).clearContent();
activeCell.offset(0,1).setValue("1");
} if (activeCell.getColumn() == 1 && activeCell.getRow() > 1 && ss.getSheetName() == "mama" && activeCell.getValue() == "Hafl") {
activeCell.offset(0,1).clearContent();
activeCell.offset(0,1).setValue("1/2");
} if (activeCell.getColumn() == 1 && activeCell.getRow() > 1 && ss.getSheetName() == "mama" && activeCell.getValue() == "No") {
activeCell.offset(0,1).clearContent();
activeCell.offset(0,1).setValue(0);
}
}
You can simplify your code this way.
(Note that I use the const variable declaration instead of var (ES6 - V8 engine))
function logic() {
const ss = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet();
const activeCell = ss.getActiveCell();
const activeCellValue = activeCell.getValue();
if (activeCell.getColumn() === 1 && activeCell.getRow() > 1 && ss.getSheetName() == "mama") {
switch(activeCellValue) {
case 'Yes':
activeCell.offset(0, 1).clearContent();
activeCell.offset(0, 1).setValue('1');
break;
case 'Half':
activeCell.offset(0, 1).clearContent();
activeCell.offset(0, 1).setValue('1/2');
break;
case 'No':
activeCell.offset(0, 1).clearContent();
activeCell.offset(0, 1).setValue('0');
break;
}
}
}
This way you only have to test the common conditions once.
Using the Switch function clearly shows the behavior of the script depending on the input value 'ActiveCellValue'.
If you need that only one action resolve per run, you need to use else if to chain the statements:
if(statement){
Action
}else if (statement2){
Action2
}else if...

init function for initializing List in Kotlin

I am trying to give init function some extra handling using the if else conditions:
val allDates = List<String>(daysInMonth) { "0$it" if(it/10 == 0) else it.toString() })
This is not a valid syntax for the init function and there seems to be very little information on this out there.
Any suggestions on how to do this?
it looks like your if statement is wrongly formated, try this
val allDates = List<String>(daysInMonth) { if(it/10 == 0) "0$it" else it.toString() }
if you want declare first array allDates
the init for allDates like this
val daysInMonth = 30
val allDates = arrayListOf<String>()
for(i in 1..daysInMonth){
if(i/10 == 0){
allDates.add("0$i")
}else{
allDates.add("$i")
}
}

Testing multiple boolean values in a single IF statement

I a newbie C++ programmer trying to test aruments/parameters passed to a program.
Multiple arguments can be passed to the program, however I want to test that if certain arguments are passed then other arguments become invalid.
e.g. PGM accepts arg(1) arg(2) arg(3) arg(4) arg(5) etc...
if arg(1) and arg(2) are supplied then arg(3), arg(4) and arg(5) etc... are invalid and the program should terminate with an error message if they are also supplied along with arg(1) and arg(2).
I've thought that using boolean IF tests would be a good way to check if certain values are true/false.
I searched on stackoverflow but not found an answer that encompasses exactly what i'm trying to do. If someone can point me in the right direction or suggest a far more efficient way of doing this I would be very grateful.
My code currently looks like this:
bool opt1 = false;
bool opt2 = false;
bool opt3 = false;
bool opt4 = false;
bool opt5 = false;
for(int i=1; i<argc; i++) {
char *str = argv[i];
if (strcmp (str, "-opt1:")==0) {opt1 = true;}
else if (strcmp (str, "-opt2:")==0) {opt2 = true;}
else if (strcmp (str, "-opt3:")==0) {opt3 = true;}
else if (strcmp (str, "-opt4:")==0) {opt4 = true;}
else if (strcmp (str, "-opt5:")==0) {opt5 = true;}
}
if((opt1) && (opt2) && (~(opt3)) && (~(opt4)) && (~(opt5)) {
** DO SOMETHING **
} else {
** DISPLAY ERROR MESSAGE AND USAGE TEXT **
}
A good solution would be using operands ! and &&
! denotes "not" (or in such case "not true") while && combines two different logical comparisons (in such case, "logic test 1" and "logic test 2")
Here's an example to do it:
if((opt1 && opt2)&&(!(opt3||opt4||opt5))){
/*
Do something if opt1 and opt2 are true and others are false
*/
}
This is practically the same as #Fareanor's solution above (first solution)
A possible fix could be (if I have well understood your problem):
if(opt1 && opt2) // opt3, opt4 and opt5 are invalid
{
if(!(opt3 || opt4 || opt5))
{
// Do something
}
else
{
// Display error message because at least opt3 or opt4 or opt5 is provided and not requested
}
}
else // opt3, opt4 and opt5 are valid
{
// Do something
}
But I think it could be better to just ignore the obsolete parameters instead of display an error while you can still run your process with only opt1 and opt2. Which could lead us to the simpler code:
if(opt1 && opt2)
{
// Do something without using opt3, opt4 and opt5
}
else
{
// Do something taking into account opt3, opt4 and opt5
}
I hope it is what you was looking for.