Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

I'm not a js guy at all... can anyone explain why this doesn't work for the guest challenge? I googled a bit and it seems like it should have.

  function guestChallenge() { 
    // code goes here
    var num = 0;
    for (var i = 1; i< 1001; i++) {
        if (i.toSring(10).match(/7/));
        else num+=i;
    }
    return num;          
  }
Edit: I really should have added more context! The challenge was:

Using the JavaScript language, have the function guestChallenge() add up all of the numbers from 1 to 1000. But everytime a number that contains a 7 is reached, don't add that number to the others (ie. exclude 7, 17, 370, etc). Do not put any code outside of the function and use the return keyword to return your answer from within the function.

And my program didn't generate any output at all, which leads me to believe I made a syntax error as opposed to just a logic error.

Edit again: Yes. I just had to fix my typing error ("toSring"), and then it passed. Thanks, guys!



s/toSring/toString/

To others, the demo question on the home page is a divisible by 5 or 7 problem. This is one of the guest challenges and is that the number contains the digit '7'.


Add when the numbers are divisible neither by 5 nor 7. And I think, you are looking for digit 7 in the numbers?


I think you missed a 't': i.toSring(10)


Thank you... I can't believe I wasn't seeing that!

I'm surely not the only one to do this sort of thing. Maybe there should be some kind of feedback instead of nothing happening when I click the run button.


Divisible by 7 or 5. Not only 7.


You are thinking of the challenge on the front page. He's talking about the first of the three "guest challenges" you get if you click the link on the front page to see more challenges.

The front page challenges asked for the sum of the integers from 1 to 1000, excluding those divisible by 5 or 7. Here was my sort of smart-ass answer:

   function jsChallenge() { 
     var num = 1000;
     var t = num;
     var sum = t*(t+1)/2;
     t = Math.floor(num/5);
     sum -= 5*t*(t+1)/2;
     t = Math.floor(num/7);
     sum -= 7*t*(t+1)/2;
     t = Math.floor(num/35);
     sum += 35*t*(t+1)/2;
     return sum;
}

The first guest challenge asks for the sum of the integers from 1 to 1000, excluding those that contain the digit 7.


If you're going to be a smart-ass, then go all out.

  function jsChallenge() {
    function sum(n) {
      var k = Math.floor(1000 / n);
      return n * k * (k + 1) / 2;
    };
    return sum(1) - sum(5) - sum(7) + sum(35);
  }




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: