# 章序号/节序号/节/笔记序号
codecademy, Condition
# 概念阐释
# 举例子
# 类比、比较与对比
# 问题
魔术八球 你已经学会了 JavaScript 中的一个强大工具:控制流!(*随机出现数字或答案*) 它是如此强大,事实上,它可以用来算命。 在这个项目中,我们将使用 JavaScript 中的控制流构建一个神奇的八球。 用户将能够输入一个问题,然后我们的程序将输出一个随机的财富。 如果您在此项目中遇到困难或希望看到有经验的开发人员完成它,请单击“摆脱困境”以观看项目演练视频。
- 从第5步开始回答
- switch中用赋值的方式写,在control flow写完之后,console eightBall
[问题步骤](https://www.codecademy.com/courses/introduction-to-javascript/projects/magic-eight-ball-1)
- [x] 1.
In the first line of the program, define a variable called `userName` that is set to an empty string.
If the user wants, they can enter their name in between the quotation marks.
- [x] 2.
Below this variable, create a **ternary expression** that decides what to do if the user enters a name or not. If the user enters a name — like `'Jane'` — use string interpolation to log `Hello, Jane!` to the console. Otherwise, simply log `Hello!`.
- [x] 3.
Create a variable named `userQuestion`. The value of the variable should be a string that is the question the user wants to ask the Magic Eight Ball. print用户想要问的问题。
- [x] 4.
Write a `console.log()` for the `userQuestion`, stating what was asked. You can include the user’s name in the `console.log()` statement, if you wish!
- [x] 5.
We need to generate a random number between 0 and 7.
Create another variable, and name it `randomNumber`. Set it equal to this expression, which uses two methods ([`Math.floor()`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Math/floor) and [`Math.random()`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Math/random)) from the Math library.
```
Math.floor(Math.random() * 8);
```
> [!hint]
>`Math.random()` returns a value between `0` (inclusive) and `1` (exclusive).
>In order to make this set of numbers range from 0 (inclusive) to 8 (exclusive) we can multiply the returned value by `8`.
>Finally, to ensure we only have whole numbers from `0` to `7` we can round down using `Math.floor()`.
- [x] 6.
Create one **more variable named** `eightBall`, and set it equal to an empty string. We will save a value to this variable in the next steps, depending on the value of `randomNumber`.
- [x] 7.
We need to create a control flow that takes in the `randomNumber` we made in step 5, and then assigns `eightBall` to a reply that a Magic Eight Ball would return. Think about utilizing `if`/`else` or `switch` statements. Here are 8 Magic Eight Ball phrases that we’d like to save to the variable `eightBall`:
- `'It is certain'`
- `'It is decidedly so'`
- `'Reply hazy try again'`
- `'Cannot predict now'`
- `'Do not count on it'`
- `'My sources say no'`
- `'Outlook not so good'`
- `'Signs point to yes'`
If the `randomNumber` is `0`, then save an answer to the `eightBall` variable; if `randomNumber` is `1`, then save the next answer, and so on. If you’re feeling creative, make your own responses!
> [!hint]
> - You can chain together multiple `else if` statements, or use a [`switch` statement](https://www.codecademy.com/resources/docs/javascript/switch) instead.
> - 使用[[js condition short-circuit 短路值]]
> - if后面的{}里不一定必须是console,也可以是变量、内置对象、逻辑运算符、数学运算符搭配使用
- [x] 8.
Write a `console.log()` to print the Magic Eight Ball’s answer, the value of the `eightBall` variable.
- [x] 9.
Run your program a few times to see random results appear in the console!
If you want extra practice:
- If you started with a `switch` statement, convert it to `if`/`else if`/`else` statements.
- If you started with `if`/`else if`/`else` statements, convert them to a `switch` statement.
# 问题答案
预期的结果是:每次点击保存后出现的字符串都不一样,就像算命一样。
```js
let userName='Jane';
userName?console.log(`hello,${userName}!`):console.log('hello!');
let userQuestion='how\'s my day?';
console.log(userQuestion);
let randomNumber=Math.floor(Math.random()*8);
console.log(randomNumber);
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;
default:
eightBall='unpredit number';
break;
}
console.log(eightBall);
```
# 备注(经验集/错误集)