简体   繁体   English

使用if / else语句的函数

[英]Function that uses if/else statement

I have created a function that will print all numbers between 1-24. 我创建了一个函数,可以打印1-24之间的所有数字。 If the number is dividable by 3, it will print Fizz, if dividable by 5, it will print Buzz. 如果该数字可被3整除,它将打印Fizz;如果该数字可被5整除,它将打印Buzz。

If start > stop, I want to add an error message. 如果开始>停止,我想添加一条错误消息。 How can I do that? 我怎样才能做到这一点? I tried with alert but that did not work out at all. 我尝试了警报,但那根本没有解决。

2) When I print the function with start= 2 and stop=24 the first value that shows up is "undefined". 2)当我以start = 2和stop = 24打印函数时,显示的第一个值是“ undefined”。 Why is that? 这是为什么?

By using .substr(1) I want to remove the first comma to the left. 通过使用.substr(1),我想删除左侧的第一个逗号。

function fizzBuzz(start, stop) {
  if (start >= stop)
 var returnString;
 for(var i= start; i <= stop; i++) {
    returnString += ",";
    if (i % 3 === 0 && i % 5 === 0)
      returnString += "FizzBuzz";
    else if (i % 3 === 0)
        returnString += "Fizz";
   else if (i % 5 === 0)
        returnString += "Buzz";
    else
        returnString += i;
}
 return returnString;
}
var result = fizzBuzz(2, 24).substr(1);

Thanks in advance! 提前致谢!

You need to put validation in if clause 您需要在if子句中放入验证

if (start >= stop)
{
   return "start must be less than stop";
}

And you need to initialize returnString with empty string first 并且您需要returnString使用空字符串初始化returnString

var returnString = "";

You want to show error message if start >= stop , it can be done using alert . 如果要start >= stopstart >= stop显示错误消息,可以使用alert来完成。 You are getting undefined for the first time because returnString variable didn't declare anywhere. 第一次没有定义,因为returnString变量未在任何地方声明。

So try something like this. 所以尝试这样的事情。 It would solve your problem 它会解决你的问题

function fizzBuzz(start, stop) {
    var returnString = '';
    if (start >= stop) {
        alert('stop must be greater than start');
        return;
    }
    for (var i = start; i <= stop; i++) {
        returnString += ",";
        if (i % 3 === 0 && i % 5 === 0) 
            returnString += "FizzBuzz";
        else if (i % 3 === 0) 
            returnString += "Fizz";
        else if (i % 5 === 0) 
            returnString += "Buzz";
        else returnString += i;
    }
    return returnString;
}

JS Fiddle Demo JS小提琴演示

you can do this . 你可以这样做 。

function fizzBuzz(start, stop) {
  if (start >= stop){
     throw "start must be less than stop";
  }
 var returnString = "";
  ...
  ...
  ...
 return returnString;
}
var result = fizzBuzz(2, 24).substr(1);

For the error mressage use throw 对于错误错误使用throw

function fizzBuzz(start, stop) {
  if (start >= stop){
     throw "start must be less than stop";
  }
  ....

This requires you catch the error if you want to display info to the user 如果您想向用户显示信息,这需要您捕获错误

try{
    var result = fizzBuzz(72, 24).substr(1);
    alert(result);
}catch(e){
    alert(e); // alerts "start must be less than stop"
}

The second question is because resultString starts life as undefined and when you concatenate a string to undefined you get the string "UndefinedYOURSTRINGHERE". 第二个问题是因为resultStringundefined开头的生命,当将字符串连接为undefined时,将得到字符串“ UndefinedYOURSTRINGHERE”。 Define returnString as an empty string. returnString定义为空字符串。

 var returnString = "";

The first value shows up is undefined because that's how you declared it: 显示的第一个值是未定义的,因为这是您声明的方式:

if (start >= stop)
 var returnString;

Notice you don't have an opening bracket, so you basically just declare that variable. 注意,您没有左括号,因此基本上只声明该变量。 Even if you don't declare it, it will still be undefined later on when you do: 即使您没有声明它,以后您仍然不能对其进行定义:

returnString += ",";

Since it always starts as undefined . 因为它总是以undefined开始。

You can move that line to the end, and also you can add an alert at the first if check and return if start > stop : 您可以将该行移到末尾,也可以在第一个if处添加警报, if check则返回,如果start > stop返回:

function fizzBuzz(start, stop) {
    if (start >= stop) {
        alert('This is wrong');
        return;
    }


  returnString = "";
  for(var i= start; i <= stop; i++) {
    if (i % 3 === 0 && i % 5 === 0)
      returnString += "FizzBuzz";
    else if (i % 3 === 0)
        returnString += "Fizz";
   else if (i % 5 === 0)
        returnString += "Buzz";
    else
        returnString += i;

    returnString += ",";
}

 return returnString;
}
var res = fizzBuzz(2, 24);
var result = res.substring(0, res.length - 1);;
console.log(result);

Fiddle 小提琴

You can use a throw statement for the error: 您可以对错误使用throw语句:

if (start >= stop){
    throw "first integer must be less than 2nd integer"
}

You must also declare returnString with an initial value: 您还必须使用初始值声明returnString:

 var returnString = "";

The problem is here, 问题在这里,

if (start >= stop) 
var returnString;

There is no need for if statement here but still if you need you can do this, 这里不需要if语句,但是如果您需要,也可以这样做,

  function fizzBuzz(start, stop) { if (start >= stop){
     var returnString;
 for(var i= start; i <= stop; i++) { returnString += ","; 
if (i % 3 === 0 && i % 5 === 0) returnString += "FizzBuzz"; 
else if (i % 3 === 0) returnString += "Fizz"; else if (i % 5 === 0) returnString += "Buzz";
 else returnString += i; }
 return returnString;
 }
    }
     var result = fizzBuzz(2, 24).substr(1);

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

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