简体   繁体   English

在fizbuz中无法摆脱逗号

[英]Cannot get rid of comma's in fizbuz

I am doing a simple fizzBuzz function. 我正在做一个简单的fizzBu​​zz功能。 The goal is to get fizz for 3 buzz for 5 and fizzBuzz for 15. The added trick is to have each one on its own line. 我们的目标是让5个热门话题为5,fizzBu​​zz为15个。另外的诀窍是让每个人都有自己的线路。

function fizzbuzz(n) {

    var array = [];
    for(var i =1; i<=n; i++) {
      if(i % 15 ===0){
            array.push('FizzBuzz');
        }
      else if (i % 3 === 0) {
            array.push('Fizz');
        }
      else if(i % 5 === 0) {
            array.push('Buzz');
        }
      else {
            array.push(i);
        }
    }
        return array + '\n'  ;
}
 console.log(fizzbuzz(15));

The code works. 代码有效。 The problem is that I am getting 问题是我得到了

console.log(1,2,fizz,4,buzz,5,etc)

I need to be getting each number on its own line. 我需要自己获得每个号码。

Just explicitly .join() your array: 只是明确地.join()您的数组:

console.log(fizzbuzz(15).join("\n"));

Passing the returned array to console.log() will implicitly turn the array into a string. 将返回的数组传递给console.log()将隐式地将数组转换为字符串。 The way that's done is effectively the same as calling .join(",") , so you get commas. 完成的方式实际上与调用.join(",") ,所以你得到逗号。 Explicitly doing the .join() yourself lets you supply any separator you want. 明确地执行.join()可以让您提供所需的任何分隔符。

edit — a smart person notes in a comment that you need to take away that + "\\n" in your return statement. 编辑 - 一个聪明的人在评论中注明你需要在你的return语句中删除 + "\\n" That right there will trigger the default .join() . 那样就会触发默认的.join() Alternatively, you could do your .join("\\n") there. 或者,你可以在那里做你的.join("\\n")

使用加入:

[1,2,'fizz',4,'buzz',5].join('\n');

you can simple "join" the array: 你可以简单地“加入”数组:

function fizzbuzz(n) {

    var array = [];
    for(var i =1; i<=n; i++) {
      if(i % 15 ===0){
            array.push('FizzBuzz');
        }
      else if (i % 3 === 0) {
            array.push('Fizz');
        }
      else if(i % 5 === 0) {
            array.push('Buzz');
        }
      else {
            array.push(i);
        }
    }
        return array.join('\n');
}
 console.log(fizzbuzz(15));

The point of fizzbuzz is to demonstrate elementary coding skill fizzbuzz的目的是展示基本的编码技巧

This exercise is testing that you can use a for loop, you understand elementary Boolean arithmetic and you can write a conditional. 本练习测试你可以使用for循环,你理解基本的布尔算法,你可以写一个条件。 That's the purpose of this exercise. 这就是本练习的目的。

If you really need everything on it's own line, just skip the array altogether and console.log('Fizzbuzz') instead. 如果你真的需要它自己的所有东西,只需完全跳过数组和console.log('Fizzbuzz') Alternately create a string and append to that, making sure to write a newline character each time round the loop. 或者创建一个字符串并附加到该字符串,确保每次循环时都写一个换行符。 Alternately, you could compose your array and join('\\n') . 或者,您可以编写数组并join('\\n')

I doubt you'd fail for not returning. 我怀疑你不会因为没有回来而失败。 That's not the spirit of fizzbuzz. 这不是fizzbuzz的精神。

To be frank though, if you're finding this hard, perhaps you are one of those people that Fizzbuzz is designed to eliminate. 坦率地说,如果你发现这很难,也许你就是Fizzbuzz旨在消除的人之一。 Harsh, sorry, there are other career options open to you. 苛刻,对不起,还有其他职业选择。 This should be a trivial 30 second job. 这应该是一个微不足道的30秒工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM