1

I got this code from codefights, and I am lack of terms to google it what this code actually does. What this function does is to get the sum of a prime number between 2 numbers which passed to parameter (a, b).

Here is the working code..

function Prime_sum(a, b){
    for(s=0; b>=a; s += b--*!c)
        for(c=b-1; b%c--; );
    return s
}

And it pops me some questions like;

  1. What's the different between using 's=0' instead of 'var s = 0'?
  2. What does this s += b--*!c do? Shouldn't this throw an error since c was not declared?
  3. If you remove the last semi-colon on the 2nd loop, this will no longer function. Why is that?
  4. If you see 'b>=a' on first loop, in which part that makes 'b < a' that makes the loop ends?

Edit: Ignore my questions and explain me what this code actually does in order.

I'm sorry if the title isn't set properly as my question.

choz
  • 17,242
  • 4
  • 53
  • 73
  • For #1, [What is the function of the var keyword and when to use it (or omit it)?](http://stackoverflow.com/questions/1470488/what-is-the-function-of-the-var-keyword-and-when-to-use-it-or-omit-it) – Jonathan Lonowski Aug 10 '15 at 01:57
  • @JonathanLonowski I just happened to read different thread than yours, but answers the same.. Thanks anyway :) – choz Aug 10 '15 at 01:59
  • #3 When you use a for loop, it has to loop something - like a code block, ";" at the end means it just loops an empty block afaik. (In your case, taking it away tells it to loop the "return s" which won't work) – Kevin P. Aug 10 '15 at 02:00

2 Answers2

2

I'll shoot:

  1. s=0 makes s a global variable
  2. Look at it as (b--) * !c // c: true = 1, false = 0
  3. The missing semicolon will eat up the statement below it and make it part of the second loop

Hope that helps.

beautifulcoder
  • 10,832
  • 3
  • 19
  • 29
0

4th one is If you see 'b>=a' on first loop, in which part that makes 'b < a' that makes the loop ends?

its

s += b--*!c

so every time b is decreasing by 1 which make this condition fulfill.

Roli Agrawal
  • 2,356
  • 3
  • 23
  • 28
  • Oh, didn't see that coming. But, this leaves me another question where c was not declared previously. But I think I just need to retrace it and learn more from this js. Thank you btw – choz Aug 10 '15 at 02:32
  • Since c wasnt declared before, shouldnt it throw an error? I backtraced it, and it threw me an error.. – choz Aug 10 '15 at 02:44
  • No its should not throw an error its global variable. so first time it will be undefined when you use ! operator with undefined it will return false and when you multiply false with number it will give 0 so first time output is 0 for s += b--*!c – Roli Agrawal Aug 10 '15 at 02:58
  • Alright, its 0 for the first loop.. I noticed on the 2nd loop has this 'c--' which should translate to 'undefined--'? and what i got from 's' in this loop is not always '0'.. Anyway, im still confused and googling what kind of sorcery this is... Any term would u recommend for me to google this? – choz Aug 10 '15 at 03:10
  • now in second line its for(c=b-1; b%c--; ); where first c is initializing with b-1 then b%c-- will happen . so let say our b is 9 then c will be initializing as 8 and then it will check for condition (b%c--) and execute this statement so it will give (9%8--) first 9%8 will execute and give ans as 1 then c-- which is 8 will become 7. – Roli Agrawal Aug 10 '15 at 03:30