I'm trying to assign a string literal to a variable using a ternary operator with the following code in the pre block:
texta = "approve";
textd = "deny";
aAction = texta eq "approve" => "true" | "false";
dAction = textd eq "approve" => "true" | "false";
However, this is what comes across in the JavaScript:
var texta = 'approve';
var textd = 'deny';
var aAction = true;
var dAction = false;
Notice that aAction and dAction should be strings, but they are actually boolean literals.
Why is this happening?
One way to force it back into a string is with a beesting:
aActionStr = "#{aAction}";
dActionStr = "#{dAction}";
Doesn't answer the question as to why this happens, but it's a hack that will work in this case.
Related
Okay, so I'm currently trying to write the code for a very inaccurate stoplight--one that is only meant to help me understand what I've learned, but I can't seem to figure it out! In my code, at the very end, I say print("Green Light!") after setting up a few variables, but I would like to indicate what the computer should do instead of flat out saying "print this phrase," if that makes sense... not sure it does.
How would I write this if I wanted to simply print the Boolean value of greenLight without saying print("Green Light!")?
I'm very much a beginner and I might be asking the wrong question--I know that, but I'm hoping someone can help!
Something tells me I haven't learned enough to do this, yet, but I really wanna know how this works.
This is what I've written so far. It runs, but I would like to change it so all I have to say is print(greenLight) or print(Bool).
When I try putting in print(greenLight), it returns an error:
Output:
Review.swift:14:7: error: variable 'greenLight' used before being initialized
print(greenLight)
^
Review.swift:4:5: note: variable defined here
var greenLight: Bool
^
var carAtRightIntersection = false
var carAtLeftIntersection = false
var carStraightAhead = true
var greenLight: Bool
if !(carAtRightIntersection && carAtLeftIntersection) && carStraightAhead {
greenLight = true
}
if carAtRightIntersection && carAtLeftIntersection && !(carStraightAhead) {
greenLight = false
}
print("Green light!")
Edit: I consulted a few coding friends, and they provided a very good solution! Provided here:
var carAtRightIntersection = false
var carAtLeftIntersection = false
var carStraightAhead = true
var colorOfLight: String = "Red"
if !(carAtRightIntersection && carAtLeftIntersection) && carStraightAhead {
colorOfLight = "Green"
}
/*
if carAtRightIntersection && carAtLeftIntersection && !(carStraightAhead) {
greenLight = false
}
*/
print(colorOfLight + " light :)")
If you don't want to print the actual phrases, You could use a switch case statement
switch greenLight{
case true:
print("Green Light!")
case false:
print("Red Light!")
default:
print("Yellow Light!")
}
After the first block executes
I have the following snippet of PowerShell:
$property = #{
Getter = { 80 };
}
$value = $property.Getter.Invoke()
$value.GetType() # Shows Name = Collection'1 instead of int
I would expect $value.GetType() to return Int32 but instead it returns a collection.
I can't just take element [0] because sometimes the Getter function will return an array.
How can I fix this?
You can strictly declare the type of the return value by this way. For example, the return type will be Double :
cls
$property = #{
Getter = [Func[Double]]{ 80 }
}
$value = $property.Getter.Invoke()
$value.GetType().Name
# Double
As you can see in the code below, I want to check if a string has a ? in his name.
If yes, I remove it.
My problem is that the variable 'nameOfFileOnlyCleaned' stay local and is empty after the if else.
Thank you for your help.
String nameOfFile = list_attachments_Of_Reference[j].toString().split('/').last;
if (nameOfFile.contains('?')) { //Removes everything after first '?'
String nameOfFileOnlyCleaned = nameOfFile.substring(0, nameOfFile.indexOf('?'));
} else{
String nameOfFileOnlyCleaned = nameOfFile;
}
//Here my variable 'nameOfFileOnlyCleaned' is empty.
This is a problem because the value should be used later
in the code. Do you know why I have this issue please?
Many thanks.
String extensionFile = nameOfFileOnlyCleaned.split('.').last;
String url_Of_File = list_attachments_Of_Reference[j].toString();
you should define your variable before if/else statement as follows:
String nameOfFileOnlyCleaned = "";
if (nameOfFile.contains('?')) { //Removes everything after first '?'
nameOfFileOnlyCleaned = nameOfFile.substring(0, nameOfFile.indexOf('?'));
} else{
nameOfFileOnlyCleaned = nameOfFile;
}
for example:
String nameOfFile = "test?test";
String nameOfFileOnlyCleaned;
if (nameOfFile.contains('?')) { //Removes everything after first '?'
nameOfFileOnlyCleaned = nameOfFile.substring(0, nameOfFile.indexOf('?'));
} else{
nameOfFileOnlyCleaned = nameOfFile;
}
print(nameOfFileOnlyCleaned);
it returns: test as a result.
I would like to print the result of the bool value
When I do it I have "true" instead the amount.
I know it probably sounds really stupid but I'm just getting started with swift
var monthsWeek:Int?
var hoursWageHours:Double = 14.47
let months4WeeksHours:Double = 156.00
let months5WeeksHours:Double = 195.00
var normalpay:Double = 0
let months5weeks:Bool = true
let months4weeks:Bool = true
if months5weeks {
normalpay = hoursWageHours * months5WeeksHours
if months4weeks {
normalpay = hoursWageHours * months4WeeksHours
}
}
or woud that make more sence even if didnt print the result still
var monthsWeek:Int?
var hoursWageHours:Double = 14.47
let months4WeeksHours:Double = 156.00
let months5WeeksHours:Double = 195.00
var normalpay:Double = 0
if monthsWeek == 195 {
normalpay = hoursWageHours * months5WeeksHours
if monthsWeek == 4 {
normalpay = hoursWageHours * months4WeeksHours
}
}
monthsWeek = 4
I came here looking for an actual print of a bool. It turns out you can do this:
var a_bool = false
print("a_bool is ")
println(a_bool)
And you can do this with ints:
var a_int = 42
println("This is an int " + String(a_int))
You can't do this with bools though. However you can do:
println("This is a bool " + String(stringInterpolationSegment: a_bool))
This is the closet I can come up with for something like this:
println("a_bool is ", a_bool) // does not work
println("a_bool is " + a_bool) // also does not work
Later on, I learned you can use \(variable) embedding like this:
println("This is an int \(a_int)")
A boolean variable can take only 2 values (true or false).
So it is logical that when you print it you have true or false.
Thanks for taking the time to look at my problem. What I'm trying to do is create a javascript function that tests whether a sting is a particular length and also whether each element of that string can be found in another string. The function then needs to return a boolean value of either true or false depending on whether the string is valid.
Here's what I have:
N_ALPHA = 6;
N_CHOICES = 4;
ALPHABET = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
var alphabet = ALPHABET.substring(0, N_ALPHA);
function isValidGuess(inStr)
{ var valid;
var Str = inStr;
for (i=0; i<Str.length; i++)
{ if (Str.charAt(i) === alphabet.charAt(i) && Str.length == N_CHOICES.length)
{ valid = true;
}
else
{ valid = false;
}
}
return valid;
}
This code is not working at all. It only returns false every time. Any help you could provide would be greatly appreciated. Thank you.
N_CHOICES.length return undefined, because variable N_CHOICES is number.
you have to change your condition to
if (Str.charAt(i) === alphabet.charAt(i) && Str.length == N_CHOICES)