I'm new to JavaScript, and decided to learn the language by running through as many of the Project Euler problems as I have time to do. I just finished the first one, and am looking for some feedback, specifically on language specific features I could use to do this faster, and faster performance overall (it seems to run a little slow) after some edits to fit the problem, it runs much faster, the document.write seemed to be really slowing the code down (probably from directly editing the page once every division of 3 or 5), and writing once value makes a huge difference.
Working fiddle here.
document.write = function (val) {
document.body.insertAdjacentHTML("beforeend", val);
}
//If we list all the natural numbers below 10 that are multiples of 3 or 5, we get 3, 5, 6 and 9. The sum of these multiples is 23.
//Find the sum of all the multiples of 3 or 5 below 1000.
var ans = [];
var k = 0;
for (var i = 1; i < 1000; i++) {
if (i % 3 === 0 || i % 5 === 0) {
ans[k] = i;
k++;
}
}
var sum = 0;
for (var i = 0; i < ans.length; i++) {
sum += ans[i];
}
document.write(sum);
//If we list all the natural numbers below 10 that are multiples of 3 or 5, we get 3, 5, 6 and 9. The sum of these multiples is 23.
//Find the sum of all the multiples of 3 or 5 below 1000.
var ans = [];
var k = 0;
for (var i = 1; i < 1000; i++) {
if (i % 3 === 0 || i % 5 === 0) {
ans[k] = i;
k++;
}
}
var sum = 0;
for (var i = 0; i < ans.length; i++) {
sum += ans[i];
}
document.write(sum);