print numbers between 1- 20 using rules

后端 未结 10 1842
不思量自难忘°
不思量自难忘° 2020-12-11 09:45

I am going through codeacademys JavaScript tutorials as i am new to it. The tutorial asks for the following:

Print out the numbers from 1 - 20.
The rules:

10条回答
  •  自闭症患者
    2020-12-11 10:20

    I don't know Javascript, but I know some Python and I've come across the same problem. Maybe this can help. I'm sure someone can just translate this for Javascript.

    Here's the original problem: Loop through the numbers between 1 and 20. If a number is divisible by 3, print Hip. If the number is divisible by 7, print “Hooray”.

    Here's the code for python for a similar problem:

    for numbers in range(1,21):
        if numbers % 3 !=0 and numbers % 7 != 0:
            print(numbers)
        if numbers % 3 == 0:
                print("Hip")
        if numbers % 7 == 0:
                print("Hooray")
    

    Here's the output:

    1 2 Hip 4 5 Hip Hooray 8 Hip 10 11 Hip 13 Hooray Hip 16 17 Hip 19 20

提交回复
热议问题