by xiaoma · 14y ago · 0 comments
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!
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'.
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.
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.
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);
}