fizzBuzz数组问题

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

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."

  1. [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:

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

Code:

  1. let someArray = []
  2. const fizzBuzz = () => {
  3. for (let i = 0; i <= 15; i++) {
  4. if (i % 3 === 0) {
  5. someArray.pop()
  6. someArray.push("fizz")
  7. } if (i % 5 === 0) {
  8. someArray.pop()
  9. someArray.push("buzz")
  10. } if (i % 15 === 0) {
  11. someArray.pop()
  12. someArray.push("fizzBuzz")
  13. } else {
  14. someArray.push(i)
  15. }
  16. }
  17. }
  18. fizzBuzz()

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

  1. let someArray = []
  2. const fizzBuzz = () => {
  3. for (let i = 0; i <= 15; i++) {
  4. if (i % 15 === 0) {
  5. someArray.push("fizzbuzz")
  6. } else if (i % 3 === 0) {
  7. someArray.push("fizz")
  8. } else if (i % 5 === 0) {
  9. someArray.push("buzz")
  10. } else {
  11. someArray.push(i)
  12. }
  13. }
  14. }
  15. fizzBuzz()

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

  1. ['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:

  1. let someArray = []
  2. const fizzBuzz = () =&gt; {
  3. for (let i = 0; i &lt;= 15; i++) {
  4. if (i % 3 === 0) {
  5. someArray.pop()
  6. someArray.push(&quot;fizz&quot;)
  7. } if (i % 5 === 0) {
  8. someArray.pop()
  9. someArray.push(&quot;buzz&quot;)
  10. } if (i % 15 === 0) {
  11. someArray.pop()
  12. someArray.push(&quot;fizzBuzz&quot;)
  13. } else {
  14. someArray.push(i)
  15. }
  16. }
  17. }
  18. fizzBuzz()

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

  1. let someArray = []
  2. const fizzBuzz = () =&gt; {
  3. for (let i = 0; i &lt;= 15; i++) {
  4. if (i % 15 === 0) {
  5. someArray.push(&quot;fizzbuzz&quot;)
  6. } else if (i % 3 === 0) {
  7. someArray.push(&quot;fizz&quot;)
  8. } else if (i % 5 === 0) {
  9. someArray.push(&quot;buzz&quot;)
  10. } else {
  11. someArray.push(i)
  12. }
  13. }
  14. }
  15. 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语句中处理零的条件...

  1. const fizzBuzz = () => {
  2. for (let i = 0; i <= 15; i++) {
  3. if (i === 0) {
  4. someArray.push(i)
  5. } else if (i % 15 === 0) {
  6. someArray.push("fizzbuzz")
  7. } else if (i % 3 === 0) {
  8. someArray.push("fizz")
  9. } else if (i % 5 === 0) {
  10. someArray.push("buzz")
  11. } else {
  12. someArray.push(i)
  13. }
  14. }
  15. }
英文:

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

  1. const fizzBuzz = () =&gt; {
  2. for (let i = 0; i &lt;= 15; i++) {
  3. if (i === 0) {
  4. someArray.push(i)
  5. } else if (i % 15 === 0) {
  6. someArray.push(&quot;fizzbuzz&quot;)
  7. } else if (i % 3 === 0) {
  8. someArray.push(&quot;fizz&quot;)
  9. } else if (i % 5 === 0) {
  10. someArray.push(&quot;buzz&quot;)
  11. } else {
  12. someArray.push(i)
  13. }
  14. }
  15. }

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:

确定