2020-05-14 初步认识JavaScript完成Magic Eight Ball输出随机文字项目

245 阅读1分钟

初步使用ES6,最让我混淆的就是,同样是console.log( )用法是什么时候该用单引号,双引号或是斜杆``呢?

于是提交到前端学习分享群,不到几分钟便有热心的前端人员详细回答,还有推荐书籍。真是感激不尽,平时大家不闲聊,有问题马上出手帮忙,氛围真是太好了。

解答

单引号和双引号一样也是普通字符 斜杠是模版字符,斜杠是es6新出的语法,为了解决"字符串"+"字符串"这种问题。

推荐看阮一峰的Es6入门教程

// Enter your name

const userName = "Alex";

userName ? console.log( Hello, ${userName} ):
console.log('Hello');

const userQuestion = 'Can you answer my question?';

console.log( The user aksed: ${userQuestion} );

let randomNumber = Math.floor(Math.random() *8);

let eightBall = ' ';

switch(randomNumber){
case 0:
eightBall = 'It is certain';
break;
case 1:
eightBall = 'It is decidedly so';
break;
case 2:
eightBall = 'Reply hazy try again';
break;
case 3:
eightBall = 'Cannot predict now';
break;
case 4:
eightBall = 'Do not count on it';
break;
case 5:
eightBall = 'My sources say no';
break;
case 6:
eightBall = 'Outlook not so good';
break;
case 7:
eightBall = 'Signs point to yes';
break;

}
console.log(eightBall);