find sum of numbers divisible by 3 or 5 and print to webpage in javascript

index.tsx
let sum = 0;
for (let i = 1; i <= 100; i++) {
  if (i % 3 === 0 || i % 5 === 0) {
    sum += i;
  }
}
document.write(`The sum of all numbers divisible by 3 or 5 from 1 to 100 is ${sum}.`);
189 chars
8 lines

In this code block, we initialize a variable called sum to 0.

Then, we loop through all numbers from 1 to 100 using a for loop.

Inside the loop, we use an if statement to check if the current number is divisible by 3 or 5 using the modulo operator %.

If it is divisible by 3 or 5, we add it to the sum variable.

Finally, we use document.write() to print the sum to the webpage, along with some text to make it readable.

gistlibby LogSnag