首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >新的Javascript和编程。我的代码出错了?

新的Javascript和编程。我的代码出错了?
EN

Stack Overflow用户
提问于 2015-07-21 21:27:05
回答 6查看 68关注 0票数 0

嗨,我是JavaScript和编程的新手。我一直在用科德罗米来帮助我学习。然而,我最近遇到了一个问题,我已经创建了一组代码,我在Codecademy中,我似乎找不到问题所在。下面是代码:

代码语言:javascript
复制
var mInQuestion = prompt('Do you play a musical instrument?').toLowerCase;
if (mInQuestion == yes) {
var musInstrument = prompt('Which instrument do you play?').toLowerCase;
switch (musInstrument) {
    case 'piano':
        console.log('Oh great! Piano is wonderful!');
        break;
    case 'guitar':
        console.log('Wow I love guitar as well!');
        experience = prompt('Did you play when you were little?').toLowerCase;
        interest = prompt('And do you like playing it?')toLowerCase;
        if (experience && interest == yes||y) {
            console.log("That's great! You must be a guitar master!");
        }
        else {
            console.log("Keep learning! It will be great. Or you could do piano?");
        }
        break;
    case 'flute':
        console.log('Oh that\'s cool! You know I used to play flute as well!');
        break;
    default:
        console.log('Oh I\'m not sure I\'ve heard of that instrument...sorry');
        break;
}
else if (mInQuestion == no) {
console.log('Oh you don\'t? You should pick one like the piano or the guitar!');
}
else {
console.log('Your answer doesn\'t make sense...');
}
EN

回答 6

Stack Overflow用户

回答已采纳

发布于 2015-07-21 21:31:36

你的吉他有点问题。您需要声明这两个变量的经验和兴趣,再加上您缺少了一个。在toLowerCase之前。您还在if语句的末尾缺少一个结束括号}。如果要测试字符串,则还需要用引号对if语句进行格式化,并且每个条件都需要完全写出。

代码语言:javascript
复制
 var mInQuestion = prompt('Do you play a musical instrument?').toLowerCase;
if (mInQuestion == yes) {
    var musInstrument = prompt('Which instrument do you play?').toLowerCase;
    switch (musInstrument) {
        case 'piano':
            console.log('Oh great! Piano is wonderful!');
            break;
        case 'guitar':
            console.log('Wow I love guitar as well!');
            var experience = prompt('Did you play when you were little?').toLowerCase; //need to declare these
            var interest = prompt('And do you like playing it?').toLowerCase;
            if ((experience ==='yes' ||experience==='y') && (interest === 'yes'|| interest ==='y')) {
                console.log("That's great! You must be a guitar master!");
            }
            else {
                console.log("Keep learning! It will be great. Or you could do piano?");
            }
            break;
        case 'flute':
            console.log('Oh that\'s cool! You know I used to play flute as well!');
            break;
        default:
            console.log('Oh I\'m not sure I\'ve heard of that instrument...sorry');
            break;
    }} //need bracket here
else if (mInQuestion == no) {
    console.log('Oh you don\'t? You should pick one like the piano or the guitar!');
}
else {
    console.log('Your answer doesn\'t make sense...');
}
票数 1
EN

Stack Overflow用户

发布于 2015-07-21 21:29:19

看看这里的这一行:

代码语言:javascript
复制
if (mInQuestion == yes) {

您正在尝试检查mInQuestion是否是字符串"yes",但实际上正在检查它是否等于不存在的变量yes。把引号放在yesno周围。

还请注意这一行:

代码语言:javascript
复制
if (experience && interest == yes||y) {

您不能检查这样的多个条件,您必须清楚地对每个检查:

代码语言:javascript
复制
if ((experience == 'yes' || experience == 'y') && (interest == 'yes' || interest == 'y'))

而且,无论您在任何地方调用toLowerCase都是不正确的。您正在尝试调用一个函数,但是省略了括号。这意味着您的变量实际上是函数,而不是您想要的字符串。每次调用toLowerCase后插入括号

票数 3
EN

Stack Overflow用户

发布于 2015-07-21 21:29:58

语法错误错过了。在这里,兴趣=提示符(‘您喜欢它吗?’)toLowerCase;

代码语言:javascript
复制
 break;
case 'guitar':
    console.log('Wow I love guitar as well!');
    experience = prompt('Did you play when you were little?').toLowerCase;
    interest = prompt('And do you like playing it?').toLowerCase;
    if (experience && interest == yes||y) {
        console.log("That's great! You must be a guitar master!");
    }
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/31549920

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档