lundi 1 janvier 2018

If statements not executing. When changed to a switch, only the default case executes

I am creating a hangman app and the if statement is not working when I remove it the code works, but only for one animal. The code should update the label, placing the letter you pressed in the appropriate place if the word contains that letter. I tried creating a switch

(random is the word that is selected from an array of animals)

switch random {
case "aardvark":
(the code for when random is aardvark)
default:
(the code for sea turtle) }

but the default case would execute every time, even when random was aardvark

@IBAction func aPressed(_ sender: Any) {

    if random == "aardvark" {
    if aardvark[0] == "a" {
        aar[0] = "a"
    };if aardvark[1] == "a" {
        aar[1] = "a"
    };if aardvark[2] == "a" {
        aar[2] = "a"
    };if aardvark[3] == "a" {
        aar[3] = "a"
    };if aardvark[4] == "a" {
        aar[4] = "a"
    };if aardvark[5] == "a" {
        aar[5] = "a"
    };if aardvark[6] == "a" {
        aar[6] = "a"
    };if aardvark[7] == "a" {
        aar[7] = "a"
    } else if aardvark[0] != "a" , aardvark[1] != "a" , aardvark[2] != "a" , aardvark[3] != "a" , aardvark[4] != "a" , aardvark[5] != "a" , aardvark[6] != "a" , aardvark[7] != "a" {
        wrong += 1
    }
        theWord.text = self.aar.joined(separator: " ")
    }

    if random == "sea turtle" {
   if seaTurtle[2] == "a" {
        sTurt[2] = "a"
        theWord.text = self.sTurt.joined(separator: " ")
   }
    }
    buttonA.isHidden = true
    updateImage()
    }

Lambda Calculus function to return TRUE/FALSE without "if"

I'm a new comer to Lambda Calculus & trying to understand its workings via Python code.


In Lambda calculus, TRUE, FALSE & IFELSE conditional can be represented as:

(I'm using L to stand for Lambda symbol)

TRUE = Lx.Ly.x
FALSE = Lx.Ly.y
IFELSE = Lc.Lx.Ly.((c x) y)

Using the above definitions, the following hold true:

(I'm writing the function applications in Python style below)

TRUE(e1)(e2) = e1
FALSE(e1)(e2) = e2
IFELSE(TRUE)(e1)(e2) = e1
IFELSE(FALSE)(e1)(e2) = e2

My attempt at creating an Lambda Calculus-style ABS function is coded below in Python. ABS applies the return value from GTZ (Greater-than-zero) repeatedly to compute abs(x).

The GTZ function uses an if statement. And since Lambda calculus doesn't have an if statement (AFAIK), how can this be implemented in Lambda calculus?

The online lambda calculus materials I reviewed all mention that IFELSE can be coded as function application; but none of them mention how the conditional input to IFELSE itself can be generated without an "if" statement or equivalent.

Please help me understand how this can be written in Lambda calculus.

Thanks!

def TRUE(x):
    def f(y):
        return x   
    return f

def FALSE(x):
    def s(y):
        return y    
    return s

def GTZ(x):
    return TRUE if x > 0 else FALSE

def ABS(x):
    return GTZ(x)(x)(-x)

Declare textfield variables to work multiple times in a if statement (swift4)

my code below is is in a textfield button. The problem is that the val1 to val3 can only be used in one if statement. How can I declare the bars to be used multiple times within that if statement.

   if  let   val1 = Int(a1.text!), let val2 = Int(a2.text!), let  val3 = Int(b1.text!)
    {
        let sum = val1 + val2
            let sum2 = val1  + val3

    }
    if a2.text == "$"{
        a1.text = String(sum2)
    }

How to properly use a scanner input as a method argument in Java?

When I try the code below it does not work, but if i explicitly put the string itself instead of a variable from the scanner it works.

Example I'm trying to fix:

import java.util.Scanner; 

public class TestFunc {
  public static void main(String [] args) {
    Scanner keyboard = new Scanner(System.in); 
    String inputz = keyboard.nextLine();
    System.out.println(testMethod(inputz));
    keyboard.close();
  }

  public static String testMethod(String input){
   if(input == "a")
      return "No!";
    else if(input == "b") 
      return "Yes!";
    else 
      return "I do not know";
  }
}

Example that works:

public class TestFunc {
 public static void main(String [] args) {
    System.out.println(testMethod("a"));
  }

 public static String testMethod(String input){
    if(input == "a")
      return "No!";
    else if(input == "b") 
      return "Yes!";
    else 
      return "I do not know";
  }
}

Oring or nesting if stament paramenter with many values

I want to include multiple options in if statement parameter for the same result but with different condition values.

I want to include a new line after printing 8 values.

This is my example:

int main(void) {

  uint8_t k;

    for (k=0;k<64;k++)
    {
        printf("%d,",k);
        if ((k == 7) && (k == 14) && (k == 21) && (k == 28) && (k == 35) && (k == 42) && (k == 49))
            printf("\n");
    }
    return 0;
}

Weird condition behaviour in PHP

Hello I've got following code:

$cleanToken = $token->cutToken($auth_header);

if($cleanToken)
{
    if($user->logout($cleanToken))
    {
        http_response_code(200);
    }
    else
    {
        http_response_code(401);
    }
    echo json_encode('true: ' . $cleanToken);
}
else
{
    echo json_encode('false' . $cleanToken);
    http_response_code(401);
}

cutToken function is returning string or false. Above code is giving me error in console

Failed to load http://localhost/obiezaca/v2/api/auth/doLogout.php: Response for preflight has invalid HTTP status code 401

I'm also not able to see any response.data although I'm consoling it out on front-end. When I delete http_response_code(401); from the last line in else error stops showing code seems to work and I'm able to see true: eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpZCI6IjIwIn0.EAEOBEDxweNnMB-iHCD5qFcn_VYKguHO8Vr3jImChhc response in my console.

So to make it clear - when I have http_response_code(401); present in else of first condition code is not working and is giving me error. When I delete this line code seems to work, it is not giving me any error and I'm able to see true ..token.. in console.

It is very weird for me because it looks like adding http_response_code there is changing $cleanToken whether it is giving true or false what is impossible.. What is going on here?

cutToken function is:

public function cutToken($auth_header)
{
    if (isset($auth_header))
    {
        $dirtyToken_trimmed = trim($auth_header);
        if (!empty($dirtyToken_trimmed))
        {
            if(preg_match('/Bearer\s(\S+)/', $dirtyToken_trimmed, $matches))
            {
                return $matches[1];
            }
            else
            {
                return false;
            }
        }
        else
        {
            return false;
        }
    }
    else
    {
        return false;
    }
}

i stuck in if condition in javascript using angular controller

enter code here
// I am stuck at if condition in angular controller
    app.controller("HomeController", function($scope, $cookies, $location, $http, $timeout, UserDetails){
    $scope.title = "fill the form to create your cv";
    $scope.step1 = true;
    $scope.step2 = false;
    $scope.step3 = false;
    $scope.step4 = false;
    $scope.submitForm1 = function(){
    alert(this.objective);
    alert(this.fullName);
    alert(this.email);
    alert(this.phoneNo);
//this if condition is not working....
    if(this.fullName == "" || this.email == "" || this.phoneNo == "" || this.objective == ""){
    $scope.title = "all fields are required";
    alert("all fields required");
    return false;
    }else{
    alert("not empty");
    }
    } 
    });