1

I have this code from JavaScript

function getURL(a, b) {
    return ++b ? String.fromCharCode((a = a.charCodeAt() + 47, a > 126 ? a - 94 : a)) : decodeURIComponent(a).replace(/[^ ]/g, this.getURL);
}

A is an encoded URL so a String, B isn't even necessary to pass in, but it can be any String.

And I have to port it over to Java for Android Development. I just don't quite get how the ternary works with the return in this function. I thought the ternary means if(before?) {Stuff before colon} else {Stuff after colon}, but I don't understand how that works in combination with the return. If I understand that it shouldn't be hard to port it to Java

batman567
  • 826
  • 2
  • 12
  • 23
rnbwSmn
  • 23
  • 5
  • 2
    It works the same in Java and javascript – Jsowa May 23 '20 at 14:05
  • Does this answer your question? [Ternary Operator - JAVA](https://stackoverflow.com/questions/25163713/ternary-operator-java) – Jsowa May 23 '20 at 14:06
  • But how does the return work with a ternary? – rnbwSmn May 23 '20 at 14:10
  • The expression on the right hand side of `return` (in this case the ternary expression with another, nested, ternary expression) is evaluated first; and the result is returned. – Ben Aston May 23 '20 at 14:11
  • `return` returns a value. In `return 2 + 2;`, `2 + 2` is an expression, i.e. it evaluates to a value. In `return condition ? trueValue : falseValue;`, `condition ? trueValue : falseValue` is an expression. `+` is an operator. `?:` is an operator. – Ry- May 23 '20 at 14:11
  • Ok thank you. I got it now.I think it would be easier if you imagine brackets around everything after the return. But that solves it – rnbwSmn May 23 '20 at 14:16

1 Answers1

0

Ok, so everything after the return is as if it were in brackets. This is solved now.

rnbwSmn
  • 23
  • 5