Kombination av if och for

status: alpha
author: krm
created: 2026-05-22

1. 🪧 count and print

const print = Number(prompt("When to print?"));
const max = Number(prompt("What should be max?"));
for(let i = 1; i < max; i++) {
  if(print === i) {
    console.log(i);
  } else {
    console.log("*");
  }
}

1. 🐾 Test 1

> 4 
> 8



2. 🐾 Test 2

> 3 
> 6





2. 🪧 To be or not to be?

const stop = Number(prompt("When to stop?"));
const max = Number(prompt("What should be max?"));
const start = Number(prompt("When to start?"));
for(let i = start; i < max; i=i+3) {
  console.log(i);
  if(stop === i) {
    console.log("Bang!");
  } 
}

1. 🐾 Test 1

> 4 
> 12
> 1





2. 🐾 Test 2

> 4 
> 12
> 2





3. 🪧 To break or not to break?

const stop = Number(prompt("When to stop?"));
const max = Number(prompt("What should be max?"));
const start = Number(prompt("When to start?"));
for(let i = start; i < max; i=i+3) {
  if(i % stop === 0) {
    break;
  } else {
    console.log(i);
  }
}

1. 🐾 Test 1

> 5 
> 100
> 3




2. 🐾 Test 2

> 100 
> 5
> 3

4. 🪧 Divided with many

const A = Number(prompt("Give a nummer A"));
const B = Number(prompt("Give a nummer B"));

for(let i = 1; i <= 10; i++) {
  if(i % A === 0) {
    console.log("A");
  } else if(i % B === 0) {
    console.log("B");
  } else {
    console.log(i);
  }
}

1. 🐾 Test 1

> 3 
> 7










2. 🐾 Test 2

> 2 
> 3