-1

In Python I use "for x in range(j)" and j is defined from user input, for example

j = int(input())
for x in range(j)
print(j)

if I input j as 3, the output will be

3
3
3

My question is, how do i do it with javascript?

I tried to do it with array, etc. Nothing seems to work, sorry im really new at coding and need to learn 2 programming language for my college

Kyanite
  • 1
  • 1

1 Answers1

0

You can do it with prompt function

const printData = () =>{

  let num = prompt("Please input a number", "3") // second parameter is the default value
  if(isNaN(num)){
    console.error(`${num} is an invalid number`)
    return
  }

 for(let i=0;i<num;i++){
    console.log(num)
  }
}

printData()
flyingfox
  • 13,414
  • 3
  • 24
  • 39
  • thank you so much for this, i've ben trying many things but you solved it right away, guess need to learn way more haha – Kyanite Nov 19 '22 at 08:20
  • oh yeah, one more questions may i ask, how do you add the output together? – Kyanite Nov 19 '22 at 08:46
  • @Kyanite check this https://stackoverflow.com/questions/28620087/print-an-output-in-one-line-using-console-log – flyingfox Nov 19 '22 at 08:50
  • i'm sorry, i mean by my question is to calculate them all togheter, like 3 +3 + 3 so the output will be 9, i know my questions is weird, its because the "3" is actually another user input that needs to be the form of total, like inputting price from many products – Kyanite Nov 19 '22 at 08:56
  • @Kyanite you can try `num**num` – flyingfox Nov 19 '22 at 08:57