把上面的小程序改成JavaScript语言编写
时间: 2024-01-05 08:04:13 浏览: 62
当然!下面是一个用JavaScript语言编写的问卷小程序的代码示例:
```javascript
console.log("欢迎参加问卷调查!");
console.log("请回答以下问题:");
// 问题1
const question1 = "您对当前的天气满意吗?";
const options1 = ["非常满意", "满意", "一般", "不满意", "非常不满意"];
console.log(`1. ${question1}`);
options1.forEach((option, index) => {
console.log(`${index + 1}. ${option}`);
});
let answer1 = prompt("请选择您的答案(输入选项的编号):");
answer1 = options1[parseInt(answer1) - 1];
// 问题2
const question2 = "您喜欢的颜色是什么?";
const options2 = ["红色", "蓝色", "黄色", "绿色", "紫色"];
console.log(`\n2. ${question2}`);
options2.forEach((option, index) => {
console.log(`${index + 1}. ${option}`);
});
let answer2 = prompt("请选择您的答案(输入选项的编号):");
answer2 = options2[parseInt(answer2) - 1];
// 问题3
const question3 = "您平时喜欢做什么运动?";
const options3 = ["篮球", "足球", "游泳", "跑步", "瑜伽"];
console.log(`\n3. ${question3}`);
options3.forEach((option, index) => {
console.log(`${index + 1}. ${option}`);
});
let answer3 = prompt("请选择您的答案(输入选项的编号):");
answer3 = options3[parseInt(answer3) - 1];
// 打印问卷结果
console.log("\n感谢您的参与!");
console.log("问卷结果如下:");
console.log(`1. ${question1}:${answer1}`);
console.log(`2. ${question2}:${answer2}`);
console.log(`3. ${question3}:${answer3}`);
```
这个JavaScript小程序会依次询问参与者三个问题,并记录他们的选择。最后,会打印出问卷结果。你可以根据自己的需求,修改问题和选项的内容。希望对你有帮助!
阅读全文