如何在 Javascript 中使用 typeof 和 switch case

发布于 12-25 08:23 字数 769 浏览 6 评论 0 原文

我无法找出下面的代码有什么问题。我咨询过如何使用 typeofswitch case,但此时我迷失了。预先感谢您的建议。

// Write a function that uses switch statements on the
// type of value. If it is a string, return 'str'. If it
// is a number, return 'num'. If it is an object, return
// 'obj'. If it is anything else, return 'other'.
function detectType(value) {
  switch (typeof value) {
    case string:
      return "str";
    case number:
      return "num";
    default:
      return "other";
  }
}

-------------- 更新 -----------------------------------

转问题出在我没有正确遵循说明的错误(或者更确切地说是疏忽)。再次感谢您的帮助和评论!

I'm having problems finding out what's wrong with the code below. I have consulted how to use typeof and switch cases, but I'm lost at this point. Thanks in advance for your advices.

// Write a function that uses switch statements on the
// type of value. If it is a string, return 'str'. If it
// is a number, return 'num'. If it is an object, return
// 'obj'. If it is anything else, return 'other'.
function detectType(value) {
  switch (typeof value) {
    case string:
      return "str";
    case number:
      return "num";
    default:
      return "other";
  }
}

------------- Update -----------------------------------

Turns out the problem comes from my mistake (or rather oversight) of not following the instructions properly. Thanks again for your help and comments!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

玩物 2025-01-01 08:23:47

typeof 返回一个字符串,所以它应该是

function detectType(value) {
  switch (typeof value) {
    case 'string':
      return "str";
    case 'number':
      return "num";
    default:
      return "other";
  }
}

typeof returns a string, so it should be

function detectType(value) {
  switch (typeof value) {
    case 'string':
      return "str";
    case 'number':
      return "num";
    default:
      return "other";
  }
}
臻嫒无言 2025-01-01 08:23:47

这是可以工作的代码。我也在学习 codeacademy.com 课程。问题在于 typeOf 具有混合大小写。它区分大小写,并且应该全部小写:typeof

function detectType(value) {
  switch(typeof value){
    case "string":
      return "str";
    case "number":
      return "num";
    case "object":
      return "obj";
    default:
      return "other";
  }
}

This is the code that will work. I'm going through the codeacademy.com coures also. The issue was with typeOf having mixed casing. It's case sensitive and should be all lowercase: typeof

function detectType(value) {
  switch(typeof value){
    case "string":
      return "str";
    case "number":
      return "num";
    case "object":
      return "obj";
    default:
      return "other";
  }
}
薆情海 2025-01-01 08:23:47

这是适合您的代码:

function detectType(value) {
  switch (typeof value) {
  case "string":
     return "str";
  case "number":
     return "num";
  default:
     return "other";
  }
}

This is the code the will work for you:

function detectType(value) {
  switch (typeof value) {
  case "string":
     return "str";
  case "number":
     return "num";
  default:
     return "other";
  }
}
水中月 2025-01-01 08:23:47

typeof 返回一个字符串,因此您应该将 switch case 括在单引号之间。


function detectType(value) {
  switch (typeof value) {
    case 'string': // string should me 'string'
      return "string";
    case 'number':
      return "number";
    default:
      return "other";
  }
}

typeof returns a string so you should encase your switch cases between single quotes.


function detectType(value) {
  switch (typeof value) {
    case 'string': // string should me 'string'
      return "string";
    case 'number':
      return "number";
    default:
      return "other";
  }
}

????

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文