fizzBuzz数组问题

huangapple go评论64阅读模式
英文:

fizzBuzz array issues

问题

I am doing a fizzbuzz challenge in JavaScript where I create a function that accepts a number and returns an array that is the length of the number. When there is a multiple of 3 it will say "fizz" and when there is a multiple of 5 it will say "buzz," and then lastly if it is a multiple of 3 and 5 it will say "fizzBuzz."

[0, 1, 'fizz', 3, 'buzz', 'fizz', 6, 7, 'fizz', 'buzz', 10, 'fizz', 12, 13, 'fizzBuzz']

I do it this way because the array starts at 0. But when I try to do this it ends up getting an output like this:

['fizzBuzz', 1, 'fizz', 3, 'buzz', 'fizz', 6, 7, 'fizz', 'buzz', 10, 'fizz', 12, 13, 'fizzBuzz']

Code:

let someArray = []

const fizzBuzz = () => {
    for (let i = 0; i <= 15; i++) {
        if (i % 3 === 0) {
            someArray.pop()
            someArray.push("fizz")
        } if (i % 5 === 0) {
            someArray.pop()
            someArray.push("buzz")
        }  if (i % 15 === 0) {
            someArray.pop()
            someArray.push("fizzBuzz")
        }   else {
            someArray.push(i)
        }
    }
}
fizzBuzz()

I have also seen where you can do it this way:

let someArray = []

const fizzBuzz = () => {
    for (let i = 0; i <= 15; i++) {
        if (i % 15 === 0) {
            someArray.push("fizzbuzz")
        } else if (i % 3 === 0) {
            someArray.push("fizz")
        } else if (i % 5 === 0) {
            someArray.push("buzz")
        }  else {
            someArray.push(i)
        }
    }
}
fizzBuzz()

This gets it right on 3 and 5, but still adds the "fizzBuzz" to the beginning of the array:

['fizzbuzz', 1, 2, 'fizz', 4, 'buzz', 'fizz', 7, 8, 'fizz', 'buzz', 11, 'fizz', 13, 14, 'fizzbuzz']

Is there something that I am doing wrong? and which way would be the correct way to do this? Or if there is a better way to do this?

英文:

I am doing a fizzbuzz challenge in JavaScript where I create a function that accepts a number and returns an array that is the length of the number. When there is a multiple of 3 it will say "fizz" and when there is a multiple of 5 it will say "buzz", and then lastly if it is a multiple of 3 and 5 it will say "fizzBuzz".


[0, 1, &#39;fizz&#39;, 3, &#39;buzz&#39;, &#39;fizz&#39;, 6, 7, &#39;fizz&#39;, &#39;buzz&#39;, 10, &#39;fizz&#39;, 12, 13, &#39;fizzBuzz&#39;]

I do it this way because the array starts at 0. But when I try to do this it ends up getting an output like this:

[&#39;fizzBuzz&#39;, 1, &#39;fizz&#39;, 3, &#39;buzz&#39;, &#39;fizz&#39;, 6, 7, &#39;fizz&#39;, &#39;buzz&#39;, 10, &#39;fizz&#39;, 12, 13, &#39;fizzBuzz&#39;]

Code:

    let someArray = []
   
    const fizzBuzz = () =&gt; {
        for (let i = 0; i &lt;= 15; i++) {
             if (i % 3 === 0) {
                someArray.pop()
                someArray.push(&quot;fizz&quot;)
            } if (i % 5 === 0) {
                someArray.pop()
                someArray.push(&quot;buzz&quot;)
            }  if (i % 15 === 0) {
                someArray.pop()
                someArray.push(&quot;fizzBuzz&quot;)
            }   else {
                someArray.push(i)
            }
        }
    }
    fizzBuzz()

I have also seen where you can do it this way:

let someArray = []

    const fizzBuzz = () =&gt; {
        for (let i = 0; i &lt;= 15; i++) {
            if (i % 15 === 0) {
                someArray.push(&quot;fizzbuzz&quot;)
            } else if (i % 3 === 0) {
                someArray.push(&quot;fizz&quot;)
            } else if (i % 5 === 0) {
                someArray.push(&quot;buzz&quot;)
            }  else {
                someArray.push(i)
            }
        }
    }
    fizzBuzz()

This gets it right on 3 and 5, but still adds the "fizzBuzz" to the beginning of the array:

[&#39;fizzbuzz&#39;, 1, 2, &#39;fizz&#39;, 4, &#39;buzz&#39;, &#39;fizz&#39;, 7, 8, &#39;fizz&#39;, &#39;buzz&#39;, 11, &#39;fizz&#39;, 13, 14, &#39;fizzbuzz&#39;]

Is there something that I am doing wrong? and which way would be the correct way to do this? Or if there is a better way to do this?

答案1

得分: 1

你可以选择从1开始循环,或者在if语句中处理零的条件...

const fizzBuzz = () => {
    for (let i = 0; i <= 15; i++) {
        if (i === 0) {
            someArray.push(i)
        } else if (i % 15 === 0) {
            someArray.push("fizzbuzz")
        } else if (i % 3 === 0) {
            someArray.push("fizz")
        } else if (i % 5 === 0) {
            someArray.push("buzz")
        }  else {
            someArray.push(i)
        }
    }
}
英文:

You can either start the loop at 1 or handle the condition for zero in the if statements...

const fizzBuzz = () =&gt; {
        for (let i = 0; i &lt;= 15; i++) {
            if (i === 0) {
              someArray.push(i)
            } else if (i % 15 === 0) {
                someArray.push(&quot;fizzbuzz&quot;)
            } else if (i % 3 === 0) {
                someArray.push(&quot;fizz&quot;)
            } else if (i % 5 === 0) {
                someArray.push(&quot;buzz&quot;)
            }  else {
                someArray.push(i)
            }
        }
    }

huangapple
  • 本文由 发表于 2023年2月6日 10:21:12
  • 转载请务必保留本文链接:https://go.coder-hub.com/75356827.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定