1

I was searching for an solution for getting the greatest prime factor of a number and found a script that works, but there is a ? (Question Mark) in the for loop. I was wondering what the ? does?

<script type="text/javascript">
    n=317584931803;
    for(i=2;n>1;n%i?i++:(n/=i,document.write(i+' ')));
</script>

And, if you can also explain what exactly this script does, I'd appreciate it.

talemyn
  • 7,110
  • 4
  • 25
  • 45

2 Answers2

5

It's known as the conditional operator. Basically, x ? y : z means if x is true, evaluate and return y, otherwise evaluate and return z. In this way, it's like an inline if/else-statement.

In this case we can break down your code like this:

n=317584931803;
for(i=2;n>1;n%i?i++:(n/=i,document.write(i+' ')));

Is equivalent to:

var n=317584931803;
for (var i=2; n>1; n % i ? i++ : (n /= i, document.write(i + ' '))) {
    // do nothing
}

But this for loop can be written more clearly as a while loop:

var n=317584931803, i = 2;
while (n > 1) {
    n % i ? i++ : (n /= i, document.write(i + ' ');
}

And the conditional operator can be expanded to:

var n=317584931803, i = 2;
while (n > 1) {
    if (n % i > 0) {
        i++;
    } else {
        n = n / i;
        document.write( i + ' ');
    }
}
p.s.w.g
  • 136,020
  • 27
  • 262
  • 299
4

Its called Conditional Operator (?:)

Complete Expression is

 test ? expression1 : expression2

Here if test condition is true, expression1 will be evaluated otherwise expression2

This is your expression

 n % i ? i++ : (n /= i, document.write(i + ' '))
Satpal
  • 126,885
  • 12
  • 146
  • 163
  • 2
    Please **stop** calling it a "ternary" operator. – zerkms Jan 27 '14 at 21:19
  • @zerkms But it IS a ternary operator http://en.wikipedia.org/wiki/%3F%3A – Justin Warkentin Jan 27 '14 at 21:24
  • 1
    @zerkms - why? If I look up `ternary` it returns this exact construct as an example. http://en.wikipedia.org/wiki/Ternary_operation – KevinDTimm Jan 27 '14 at 21:25
  • @KevinDTimm: "Since this operator is often the only existing ...". You don't call `++` an unary operator don't you? – zerkms Jan 27 '14 at 21:28
  • 1
    @Justin Warkentin: when another ternary operator will be introduced in a language you'll experience a lot of issues with terminology. Why not follow the correct naming from the very beginning? – zerkms Jan 27 '14 at 21:28
  • @zerkms - Yeah, when there's only one of something, normal humans refer to that thing as though there's only one of that thing. It's not that weird. – Wayne Jan 27 '14 at 21:29
  • @lwburk: "normal" is subjective. http://blogs.msdn.com/b/ericlippert/archive/2010/02/18/whats-the-difference-between-ternary-and-tertiary.aspx – zerkms Jan 27 '14 at 21:29
  • 1
    @zerkms Regardless, it is widely known as and called "the ternary operator". So far, I'm not aware of any other ternary operators. When that magical day comes in a popular language, I'm sure we'll differentiate them with their proper names. Technically "conditional operator" isn't really that explicit either. – Justin Warkentin Jan 27 '14 at 21:30
  • @Justin Warkentin: it is explicit since it's assigned in an unique manner to a `?:` language (syntax) construction. – zerkms Jan 27 '14 at 21:32
  • Yes, it's also my subjective opinion that you're being silly. – Wayne Jan 27 '14 at 21:34
  • @lwburk: that's not polite. Anyway, if one doesn't want to use a proper name - it's their business. Please continue spreading a bs, amen. – zerkms Jan 27 '14 at 21:35
  • @zerkms Just out of curiosity, are you aware of any language which has more than one ternary operator? – p.s.w.g Jan 27 '14 at 21:36
  • @p.s.w.g: not of any I work with every day. Haven't made any research on that, but if it's that important it's not that difficult to provide a BNF for the language that would provide 2 ternary operators. Or a single ternary operator that doesn't behave like in another languages. – zerkms Jan 27 '14 at 21:38
  • Please refer to *the* President of the United States as *a* President of the United States, because, theoretically, we could add a second president at some point. – Wayne Jan 27 '14 at 21:41
  • @lwburk: okay, I will. Now your turn to start using proper names. – zerkms Jan 27 '14 at 21:42
  • @lwburk: http://www.ecma-international.org/ecma-262/5.1/#sec-11.12 you're welcome. Not even a **SINGLE WORD** about "ternary" in the whole specification. Any further questions? – zerkms Jan 27 '14 at 21:44
  • You're confused about how language works. I use sounds and symbols to unambiguously refer to things for the benefit of other humans. I never claimed to be quoting a spec. I also don't see the words "immediately invoked function expression" anywhere in the spec. Or even the word module. We name things outside the confines of the spec *all the time*. – Wayne Jan 27 '14 at 21:50
  • This reminds me of one time when I used the word *context* in its normal English sense and somebody yelled at me because *context* wasn't in the spec. I had to explain that I was just using English to describe something. Programmers can be weird about this stuff. – Wayne Jan 27 '14 at 21:51
  • @lwburk: 1. It is defined by specification, I don't see any reason to not use the official (see "proper") name 2. The specification doesn't define IIFE just because it's not a single language construct - it's a function expression followed by a function invocation. "We name things outside the confines of the spec all the time" --- it's a science. It's a good idea to use the terms that are defined in a trustable source. Otherwise one day you'll advocate using "that thing" instead of "IIFE" – zerkms Jan 27 '14 at 21:51