Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/471.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 从工厂返回正确值_Javascript_Factory - Fatal编程技术网

Javascript 从工厂返回正确值

Javascript 从工厂返回正确值,javascript,factory,Javascript,Factory,试图在JavaScript中创建一个工厂,当我向工厂传递一个值时,我希望它返回正确的字符串,而不是整数 function Factory(amount){ switch (amount) { case amount == 1: return "one " break; case amount == 2: return "two " break; case amount > 2: return "more

试图在JavaScript中创建一个工厂,当我向工厂传递一个值时,我希望它返回正确的字符串,而不是整数

function Factory(amount){
  switch (amount) {
    case amount == 1:
      return "one "
      break;
    case amount == 2:
      return "two "
      break;
    case amount > 2:
      return "more than two "
      break;
  }
  return amount;
}

var per1 = Factory(1);
console.log(per1, 'per1');
var per2 = Factory(2);
var per3 = Factory(3);
但当我每1次注销时,得到的值是:1而不是“1”

我有什么做得不太对

      function Factory(amount){
          switch (amount) {
            case 1:
              return "one "

            case 2:
              return "two "

            default:
              return "more than two "

        }
    }
像这样改变你的情况

试试这个


开关大小写对值有效,而不是对真/假

你也不需要休息,因为你已经回来了


如果上面没有提到,那么缺省情况将处理其余情况。因此,您也不需要返回金额作为备用。

您不需要在返回后中断。我相信w3schools是这样教的->
function Factory(amount){
  switch (amount) {
    case 1:
      return "one "
      break;
    case 2:
      return "two "
      break;

  }
  return amount;
}



console.log(Factory(1))
//output "one"
function Factory(amount){
  switch (amount) {
    case 0:
      return "zero "
    case 1:
      return "one "
    case 2:
      return "two "
    default:
      return "more than two "
  }
}