Error: a 'return' expression required in a function with a block body ('{...}'). What is wrong? - function

I have an error "a 'return' expression required in a function with a block body ('{...}')" in a code snippet:
fun validateMove(nam: String): Boolean {
for (i in 1..8) {
if (nam == first) {
if (Regex("[a-h][0-9][a-h][0-9]").matches(turn)) { return true
} else { return false }
} else if (Regex("[i-m][0-9][i-m][0-9]").matches(turn)) { return true
} else return false
}
}
I tried to use several modifications of this code, did it in other variants with different position of "{}", but it still doesn't work. I have no idea what problem is. Will be very thankfull if somebody can help me.

There is no return outside the for so if the for does 0 loop (I know its not supposed to happen here but that's how computers think), there won't be any returns. I you create a variable 'result', and store the result in it and then, at the very end of the function put a return result, it should work.
(If I read your function correctly, it is supposed to return something on the first loop so I don't really understand why there is a loop here.)

Related

How to access specific value in this 'json'?

I'm accessing an URL like http://localhost/wp-admin/admin-ajax.php?action=process
and it returns something like this :
{
"status": "processing",
"icon": "test"
"testing": 0
}
I've tried everything to get the status but it never works. I precise that it is an Ionic app.
Here is my code (in console.log, everything that I've tried):
process() {
let url: any = 'http://localhost/wp-admin/admin-ajax.php?action=process'
this.httpIonic.get(url, null, null)
.then(response => {
console.log(response.data);
console.log(response.data[0].status)
console.log(response.data[0]['status'])
console.log(response.data.data.status)
console.log(response.data.status)
console.log(response[0]['status']);
console.log(response[0].status)
console.log(response[0])
console.log(response.status)
})
}
It always returns either nothing, either everything.
What am I doing wrong ?
Thanks
standing their documentation ‘response.status’ should be the right one... I would add the catch so to get more info if something’s going wrong;
furthermore I would even try to replace both null with empty objects
After getting the response. put that value in in a variable. after that try to access it. it will work.
example
this.val = result;
this.MediaList=this.val.problemActionList;
value.MediaList=this.MediaList.MediaList;
Try using JSON.parse(response) and then refer to response.status.

Ignore Undefined objects in array of arrays variable

In Google Apps Script, I'm pulling data from an API. In the code below, the "output" variable contains an array of arrays. There is always at least one ["Response"] object, but sometimes there are 2.
My problem is, the code below isn't returning the second object (["Response"][1]) when it is present. I tried removing the "if" statement, but I get an error saying "TypeError: Cannot read property "Product" from undefined".
Does anyone know how to get the second object when it's present and ignore it when it's not?
var data = reportAPI();
var applications = data["applications"];
var output = []
applications.forEach(function(elem,i) {
output.push(["ID",elem["Id"]]);
output.push([elem["Response"][0]["Product"],elem["Response"][0]["Status"]]);
if (["Response"][1] != null) {
output.push([elem["Response"][1]["Product"],elem["Response"][1]["Status"]]);
}
}
P.S. I would even be happy with replacing the undefined object with "", but I'm not sure how to do it.
How about this modification? Please think of this as one of several answers. I used forEach to elem["Response"]. By this, values can be pushed by the number of elem["Response"].
From :
applications.forEach(function(elem,i) {
output.push(["ID",elem["Id"]]);
output.push([elem["Response"][0]["Product"],elem["Response"][0]["Status"]]);
if (["Response"][1] != null) {
output.push([elem["Response"][1]["Product"],elem["Response"][1]["Status"]]);
}
}
To :
applications.forEach(function(elem) {
output.push(["ID",elem["Id"]]);
elem["Response"].forEach(function(elem2) {
output.push([elem2["Product"],elem2["Status"]]);
});
});
If this didn't work, please tell me. I would like to modify.
The example below helps to account for the cases where the Response[0] or Reponse[1] are not "undefined" or "null". Putting !() will turn the Boolean values for "undefined" or "null" to true.
applications.forEach(function(elem,i) {
output.push(["ID",elem.Id]);
if(!(elem.Reponse[0]))
output.push([elem.Response[0].Product,elem.Response[0].Status]);
if (!(elem.Response[1])) {
output.push([elem.Response[1].Product,elem.Response[1]Status]);
}
}

How to pass argument values to parameters inside of a objects method as a function

Lets say I have a a object and inside that object I have a function as a property in other words a method, proper JavaScript terminology and with that function I have parameters. How could I pass argument values to that functions parameters would I invoke the argument inside the object or outside the object. Help.
var learningDaily = {
good: true,
smarterDaily: true,
makingMoney: false,
needHelp: true
question: function (question, please) {
if (question typeOf === Number) {
alert("JUST TELL ME HOW TO PASS ARGUMENTS TO PARAMS IN OBJ");
} else if (please === "thank you") {
alert ("thanks")
} else {
alert("thank again");
}
},
getSmarterAfterThis: true
}
Sorry I may have been missing some closing tags bu, pretty much the question is straight forward. Would I invoke the arguments inside the object or outside the object.
help(23, "thank you");
There are several syntax errors in your javascript. It's not clear what the method is trying to accomplish. I gather English is not your first language. I'm afraid the question as-written isn't very clear. That's probably why you're having some trouble getting an answer.
Here's an example that ought to get you going in the right direction:
obj = {
value_label: 'The value of x is: ',
type_label: '. Its type is ',
fun: function(x) {
// Show the value of x with it's type and some label text.
alert(this.value_label + x + this.type_label + typeof(x) + '.')
}
};
// Call the method member fun in obj
obj.fun(42)
You can paste this into https://jsfiddle.net and run it.

Function returns two values and both of them are returned

Inside my webpack.config I use webpack-assets-manifest plugin to generate manifest json file. I need to change the key name inside json object so I customize property following instructions in the docs and this works perfect. What I wonder about is how is this working? Inside my manifest.json file localBundle and localBundle.map are generated. Isn't function supposed to end after it hits first return? How come both if statements return something?
webpack.config.js
module.exports = {
...
new WebpackAssetsManifest({
customize: (key, value) => {
if (value.toLowerCase().endsWith('.local.css')) {
return {
key: 'localBundle',
value: value
}
}
if (value.toLowerCase().endsWith('.local.css.map')) {
return {
key: 'localBundle.map',
value: value
}
}
}
}),
...
}
manifest.json
{
...
"localBundle": "stylesLocal.b035cc665aee76e41676ad101e93fd67.local.css",
"localBundle.map": "stylesLocal.b035cc665aee76e41676ad101e93fd67.local.css.map",
}
The customize callback runs multiple times which is why your conditional return values are used in manifest.json.
If you're curious, here is where customize is called.

How to break foreach loop in xtend?

I have below code : Which is iterating the arrayList till end even though I don't want it. I don't know how to break it as xtend does not have break statement. Provided I can't convert the same to a while loop, is there any alternative way in xtend similar to break statement in java?
arrayList.forEach [ listElement | if (statusFlag){
if(monitor.canceled){
statusFlag = Status.CANCEL_STATUS
return
}
else{
//do some stuff with listElement
}
}]
You can try something like that
arrayList.takeWhile[!monitor.cancelled].forEach[ ... do stuff ...]
if ( monitor.cancelled ) { statusFlag = Status.CANCEL_STATUS }
takeWhile is executed lazily, so it should work as expected and break at correct moment (memory visibility allowing, I hope monitor.cancelled is volatile).
Not sure how status flag comes into picture here, you might need to add check for it in one or both closures as well.
You are right, break and continue is not supported by Xtend.
Because it seems that you would like to 'break' based on an external condition (so you can't use e.g. filtering) I think it's not a bad option to throw an exception.
Pseudo code:
try {
arrayList.forEach[
if (monitor.canceled) {
throw new InterruptedException()
}
else {
// Continue processing
}
]
}
catch (InterruptedException e) {
// Handle
}
You are right, break and continue is not supported by Xtend.
Move break specific functionality to java and use that in xtend.