repl.it question "lovesPizza" in control flow coding giving me trouble - control-flow

It says that I'm missing a ";" on line 5 else(typeOfPizza === "olives"){
function exerciseThree(typeOfPizza){
let lovesPizza;
if (typeOfPizza === "pepperoni"){
return "true";}
else(typeOfPizza === "olives"){
return "false";
}

Where you are using a condition for else it actually needs to be else if like this:
function exerciseThree(typeOfPizza){
let lovesPizza;
if (typeOfPizza === "pepperoni"){
return "true";}
else if (typeOfPizza === "olives"){
return "false";
}
}
You cannot have else(condition) the else if statement needs to be if(condition) {} else {} if you require a condition for the else then you need to do if(condition) {} else if(condition) {}

Related

Structure or syntax for empty if/elsif/else block in perl

This amounts to testing for xor (one or the other but not both and not neither) where the task demands something is done for the one but not for the other but we abandon the project if both or neither.
This example might be most to the point but the empty block doesn't sit well with me
if (condition == 1) {
do something
}
elsif (condition == 2) {
; # do nothing aka carry on
}
else {
exit;
}
Nesting seems no better and in this example misses (ie, does not validate) the second condition
if (condition) {
if (condition == 1) {
do something
}
}
else {
exit;
}
Combining both of the above avoids the empty block and will work but forces redundant testing and seems inefficient.
if ((condition == 1) || (condition == 2)) {
if (condition == 1) {
do something
}
}
else {
exit;
}
Is there an alternative I haven't thought of? Why might one choose one structure over another? Thanks for your insights.
if (condition == 1) {
do something
}
elsif (condition == 2) {
; # do nothing aka carry on
}
else {
exit;
}
and
if ((condition == 1) || (condition == 2)) {
if (condition == 1) {
do something
}
}
else {
exit;
}
are equivalent to
if (condition == 1) {
do something
}
elsif (condition != 2) {
exit;
}
None of the above test for "one or the other but not both and not neither". For that, you'd need the following:
if (condition == 1 || condition == 2) {
do something
}
Since condition can't be both 1 and 2, no further checks are needed. On the other hand, if you had two independent conditions, you could literally use xor.
if (condition1 xor condition2) {
do something
}
Warning: This is a low-precedence operator (like not, and and or), so you may need to put parens around both of the expressions it has for operands.
my %dispatch = {
1 => sub { dosomething() },
2 => sub { dosomethingelse() },
}
exists $dispatch{$condition} ?
$dispatch{$condition}->() :
do_default();

Why is my dataweave map code not working?

I'm trying to convert some code from mule 3 to mule 4 so I'm using dataweave 2. I keep getting errors from this code
keySets: if(payload.autoQuote !=null) payload.autoQuote map (autoQuote) -> {
(if(payload.autoQuote.personalAuto.vehicles != null) autoQuote.personalAuto.vehicles map ((vehicles,indexOfvehicle) ->{
keySet: {
key: if(vehicles.usage != null) vehicles.usage else 'DNF' ,
keySequence: "TAUTO_USE_TYPE" ++ '_' ++ indexOfvehicle
}
} else {}))
} else {}
On the first line I'm getting
mismatched input '->' expecting 'else'
on the second line I'm getting
- missing EOF at 'payload'
and
- no viable alternative at input '('
on the first two '('
I believe keySets is a list and not a object, you should be using [] and not {} in your else statements. I edited some of the braces, and it is compiling fine for me, though not sure if it will work as expected for you.
keySets: if(payload.autoQuote !=null) (payload.autoQuote map (autoQuote) -> {
(if(payload.autoQuote.personalAuto.vehicles != null)
autoQuote.personalAuto.vehicles map ((vehicles,indexOfvehicle) -> {
keySet: {
key: if(vehicles.usage != null) vehicles.usage else 'DNF',
keySequence: "TAUTO_USE_TYPE" ++ '_' ++ indexOfvehicle
}
}) else [])
}) else []
Give it a try and let me know if it worked.
This doesn't give me errors, though I'm not sure if it makes sense:
%dw 2.0
output application/json
---
{
keySets: if(payload.autoQuote !=null) (payload.autoQuote) map(autoQuote, i) -> {
( if(payload.autoQuote.personalAuto.vehicles != null) (autoQuote.personalAuto.vehicles) map (vehicles,indexOfvehicle) ->{
keySet: {
key: if(vehicles.usage != null) vehicles.usage else 'DNF' ,
keySequence: "TAUTO_USE_TYPE" ++ '_' ++ indexOfvehicle
}
} else {})
} else {}
}
You should give an example of the input and the expected output.
Also I'm not sure why you don't use the autoQuoteparameter of the map inside of the map instead of payload.autoQuote...
Turns out I had the parentheses wrong. The first else was in the parentheeses after the map. It should look like this. Don't know if it works but it at least compiles.
keySets: if(payload.autoQuote !=null) payload.autoQuote map (autoQuote) -> {
(if(payload.autoQuote.personalAuto.vehicles != null) autoQuote.personalAuto.vehicles map ((vehicles,indexOfvehicle) ->{
keySet: {
key: if(vehicles.usage != null) vehicles.usage else 'DNF' ,
keySequence: "TAUTO_USE_TYPE" ++ '_' ++ indexOfvehicle
}
}) else {})
} else {}

Cant reach return statement in a boolean

I am trying to reach the second return statement. For whatever reason, the code stops after reaching the first if statement. What am I doing wrong?
function orderFood(food) {
if (true){
return 'chinese';
}
else if (false){
return 'italian';
}
}
orderFood(false);
You will never reach to the second statement. The if else statement is expecting a variable whose value to be matched. If you hard coded the true then the else if never be working. I suggest you to understand the basic of if-else.
function orderFood(food) {
//here food is a boolean variable and if else will look for this
if (food){
return 'chinese';
} else{
return 'italian';
}
}

Swift 3 enum with associated value AND function comparison

I have this struct that has an enum property as well as a function:
struct UserInput {
enum State {
case unrestricted
case restricted(because: WarningType)
enum WarningType {
case offline
case forbidden
}
}
var config: UserInputConfig?
var state: State = .unrestricted
func isConfigured() -> Bool {
// Arbitrary checks about the config...
}
}
Is there a way to rewrite the following conditionals so that the check for isConfigured() and state are in the same statement?
if case .restricted = userInput.state {
return 1
} else if userInput.isConfigured() {
return 1
} else {
return 0
}
It seems because the State enum uses associated values, you cannot simply write if userInput.state == .restricted || userInput.isConfigured(), you need to use the if case syntax. There must be a way around this?
You would like to do this:
if case .restricted = userInput.state || userInput.isConfigured() {
return 1
} else {
return 0
}
but there is currently no way to do an OR with pattern matching. There are a couple of ways of doing AND.
By using DeMorgan's Laws, you can turn if a || b into if !(!a && !b) and by reversing the then and else clauses of your if statement, you can just check for if !a && !b.
Unfortunately, you can't say if !(case .restricted = userInput.state), but since your enum has only 2 cases, you can replace that with if case .unrestricted = userInput.state.
Now, how do you use that with another statement? You can't use && for the same reason you can't use ||.
You can check for the failing case by using a pattern that matches both failing conditions (which is using AND) and then return 1 if both failing conditions aren't met:
if case (.unrestricted, false) = (userInput.state, userInput.isConfigured()) {
return 0
} else {
return 1
}
Equivalently you can use a multi-clause condition:
if case .unrestricted = userInput.state, !userInput.isConfigured() {
return 0
} else {
return 1
}
In addition to being shorter and IMO easier to read, this second method can short circuit and skip calling userInput.isConfigured in the case where case .unrestricted = userInput.state fails.
You can do it really cleanly with a switch statement, and pattern matching:
switch userInput.state
{
case .unrestricted:
return userInput.isConfigured() ? 1 : 0;
case .restricted(_):
return 1
}

Issue using .Contains with application.loadedlevel/loadlevel

I'm working on a script that returns to the correct scene when pressing escape.
In the DataUpgrade, Level Select, TransferData(x) it should return to StartMenu, in StartMenu it should close the game and in a Level(x) it should return to TransferData(x).
The following script has been made with help from FunctionR.
#pragma strict
import System;
function Update () {
if (Input.GetKey ("escape"))
{
var level : String = Application.loadedLevel.ToString();
//var transferData = "TransferData";
if(level == "StartMenu"){
Application.Quit();
}
else if(level == "DataUpgrade"){
Application.LoadLevel("StartMenu");
}
else if(level == "Level Select"){
Application.LoadLevel("StartMenu");
}
else if(level.Contains("TransferData"))
Application.LoadLevel("StartMenu");
else if(level.Contains("Level"))
Application.LoadLevel (Application.loadedLevel - 1);
//trying this out but isn't working either
/*else if( transferData in level)
Application.LoadLevel("StartMenu");
else if("Level" in level)
Application.LoadLevel (Application.loadedLevel - 1);*/
}
}
i'm not sure why it's not working, the scenes have the correct names and have also been added to build settings.
Thanks in advance
Quick Fix
This is how you fix it, and you don't need the for loop. I use Contains() to do the hard work.
import System;
function Update ()
{
if (Input.GetKey ("escape"))
{
var level : String = Application.loadedLevel.ToString();
if(level == "StartMenu")
Application.Quit();
else if(level == "DataUpgrade")
Application.LoadLevel("StartMenu");
else if(level == "Level Select")
Application.LoadLevel("StartMenu");
else if(level.Contains("TransferData")
Application.LoadLevel("StartMenu");
}
}
Smart Fix
You can also be smart and take advantage of the OR operator to reduce your related if statements.
else if(level == "DataUpgrade" || level == "Level Select" || level.Contains("TransferData")
Application.LoadLevel("StartMenu");
Sneaky Fix
You can also be sneaky since all other cases, except one, takes you back to StartMenu. You can have an else that guarantees a return to StartMenu.
if(Application.loadedLevel == "StartMenu")
Application.Quit();
else
Application.LoadLevel("StartMenu");
Replacing this line:
var level : String = Application.loadedLevel.ToString();
with
var level = Application.loadedLevelName;`
fixed the problem, everything is working now