- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在做一个测验,作为更大的测验网站的一部分。基本思想是,当用户单击包含问题对象生成的文本的答案时,对象中关联的“类型”字符串将添加到状态“分数”数组中。最后,我使用 for 循环来确定最常选择哪个答案字符串,并给出结果。
我的问题是分数数组无法正确填充这些字符串,而且我不完全确定发生了什么。我知道您不应该尝试直接更改状态,而应该在 setState 之外进行转换,或者使用迭代。
当我尝试使用迭代时 - 又名类似这样的东西:
export default class Quiz3 extends React.Component {
constructor(props) {
super(props)
this.state = {
clickBegin: false,
scoredQuiz: false,
currentQuestion: 0,
score: [],
mostFrequent: '',
}
this.functionA = this.functionA.bind(this);
}
functionA () {
var stateScore = this.state.score;
if(this.state.currentQuestion < 7) {
this.setState ({
score: [...stateScore, questions[this.state.currentQuestion].choiceA.type],
currentQuestion: this.state.currentQuestion + 1
})
}
else if(this.state.currentQuestion >= 7) {
this.scoreQuiz();
}
return;
}
我收到一个 TypeError,指出 this.state.score 无法迭代。
好的。因此,我尝试使用 .concat 在 setState 之外操作它(也尝试过推送,但直接操作状态,这会导致问题)。
export default class Quiz3 extends React.Component {
constructor(props) {
super(props)
this.state = {
clickBegin: false,
scoredQuiz: false,
currentQuestion: 0,
score: [],
mostFrequent: '',
}
this.functionA = this.functionA.bind(this);
}
functionA () {
var stateScore = this.state.score;
if(this.state.currentQuestion < 7) {
var newScore = stateScore.concat(questions[this.state.currentQuestion].choiceA.type)
this.setState ({
score: newScore,
currentQuestion: this.state.currentQuestion + 1
})
}
else if(this.state.currentQuestion >= 7) {
this.scoreQuiz();
}
return;
}
那就说明 TypeError 是 stateScore.concat() 不是一个函数。我不知道必须如此,也不知道如何纠正该错误。
长话短说,我试图用外部对象的值填充分数状态数组,但我做不到。我还制作了更多函数,但为了简单起见,我想将它们排除在这些主要示例之外。这是完整的代码,以防我错过了什么。 (我知道函数 B-D 正在使用推送。我打算稍后从函数 A 复制代码)
quiz3.js(React 应用程序本身):
import React from 'react';
import TopNavbar from '../navbar';
import {startLabels} from './javascript/javascript-quiz-3';
import {questions} from './javascript/javascript-quiz-3';
import {endLabels} from './javascript/javascript-quiz-3';
import {Container} from 'react-bootstrap';
import {Row} from 'react-bootstrap';
import {Col} from 'react-bootstrap';
export default class Quiz3 extends React.Component {
constructor(props) {
super(props)
this.state = {
clickBegin: false,
scoredQuiz: false,
currentQuestion: 0,
score: [],
mostFrequent: '',
}
this.reset = this.reset.bind(this);
this.changeClicked = this.changeClicked.bind(this);
this.scoreQuiz = this.scoreQuiz.bind(this);
this.functionA = this.functionA.bind(this);
this.functionB = this.functionB.bind(this);
this.functionC = this.functionC.bind(this);
this.functionD = this.functionD.bind(this);
}
// State Functions
reset() {
this.setState({
currentQuestion: 0,
score: 0,
})
}
changeClicked() {
if (this.state.clickBegin === false) {
this.setState({
clickBegin: true
})
}
else {
this.setState({
clickBegin: false
})
console.log(this.state.clickBegin)
}
}
scoreQuiz() {
if(this.state.currentQuestion === 7) {
if(this.state.scoredQuiz === false) {
this.setState({
scoredQuiz: true
})
}
for(var i = 0; i > this.state.score.length; i++) {
var counts = {};
var compare = 0;
var result = this.state.score[i];
var mostFrequentValue = undefined;
if (counts[result] === undefined) {
counts[result] = 1
}
else {
counts[result] = counts[result] + 1
}
if (counts[result] > compare) {
compare = counts[result];
mostFrequentValue = this.state.score[i];
}
}
this.setState({
mostFrequent: this.state.score[mostFrequentValue]
})
}
else {
this.setState({
scoredQuiz: false
})
}
}
functionA () {
var stateScore = this.state.score;
if(this.state.currentQuestion < 7) {
var newScore = stateScore.concat(questions[this.state.currentQuestion].choiceA.type)
this.setState ({
score: newScore,
currentQuestion: this.state.currentQuestion + 1
})
}
else if(this.state.currentQuestion >= 7) {
this.scoreQuiz();
}
return;
}
functionB () {
if(this.state.currentQuestion < 7) {
this.setState ({
score: this.state.score.push(questions[this.state.currentQuestion].choiceB.type),
currentQuestion: this.state.currentQuestion + 1
})
}
else if(this.state.currentQuestion >= 7) {
this.scoreQuiz();
}
return;
}
functionC () {
if(this.state.currentQuestion < 7) {
this.setState ({
score: this.state.score.push(questions[this.state.currentQuestion].choiceC.type),
currentQuestion: this.state.currentQuestion + 1
})
}
else if(this.state.currentQuestion >= 7) {
this.scoreQuiz();
}
return;
}
functionD () {
if(this.state.currentQuestion < 7) {
this.setState ({
score: this.state.score.push(questions[this.state.currentQuestion].choiceD.type),
currentQuestion: this.state.currentQuestion + 1
})
}
else if(this.state.currentQuestion >= 7) {
this.scoreQuiz();
}
return;
}
// Different pages rendered depending on state values
render() {
if (this.state.clickBegin === false && this.state.scoredQuiz === false) {
return (
<div>
<TopNavbar />
<div id = "main">
<StartScreen
clickBegin = {this.state.clickBegin}
scoredQuiz = {this.state.scoredQuiz}
currentQuestion = {this.state.currentQuestion}
score = {this.state.score}
changeClicked = {this.changeClicked}
reset = {this.reset} />
</div>
</div>
)
}
if (this.state.clickBegin === true && this.state.scoredQuiz === false) {
return (
<div id = "main">
<TopNavbar />
<QuestionsScreen
clickBegin = {this.state.clickBegin}
scoredQuiz = {this.state.scoredQuiz}
currentQuestion = {this.state.currentQuestion}
score = {this.state.score}
functionA = {this.functionA}
functionB = {this.functionB}
functionC = {this.functionC}
functionD = {this.functionD}
/>
</div>
)
}
if (this.state.clickBegin === true && this.state.scoredQuiz === true) {
return (
<div>
<TopNavbar />
<div id = "main">
<EndScreen
currentQuestion = {this.state.currentQuestion}
score = {this.state.score}
mostFrequent = {this.state.mostFrequent}
scoreQuiz = {this.scoreQuiz}
reset = {this.reset}
/>
</div>
</div>
)
}
else {
return null;
}
}
}
class StartScreen extends React.Component {
constructor(props) {
super(props)
this.click = this.click.bind(this);
}
click() {
this.props.changeClicked();
this.props.reset();
}
render() {
return (
<Container fluid id = 'start-screen'>
<Row id = "quiz-title">
<h1> {startLabels[0].title} </h1>
</Row>
<Row>
<img src = {startLabels[0].imgSrc} />
</Row>
<Row id = "quiz-description">
<p> {startLabels[0].descrip} </p>
</Row>
<Row id = "begin-quiz" onClick = {this.click}>
<p> {startLabels[0].startQuiz} </p>
</Row>
</Container>
)
}
}
class QuestionsScreen extends React.Component {
render() {
return (
<Container fluid id = "questions-screen">
<Row id = "question-title">
<p> {questions[this.props.currentQuestion].question} </p>
</Row>
<Row id = "choice-container-1">
<Col id = "choiceA" onClick = {this.props.functionA}> <p> {questions[this.props.currentQuestion].choiceA.choice} </p> </Col>
<Col id = "choiceB" onClick = {this.props.functionB}> <p> {questions[this.props.currentQuestion].choiceB.choice} </p> </Col>
</Row>
<Row id = "choice-container-2">
<Col id = "choiceC" onClick = {this.props.functionC}> <p> {questions[this.props.currentQuestion].choiceC.choice} </p> </Col>
<Col id = "choiceD" onClick = {this.props.functionD}> <p> {questions[this.props.currentQuestion].choiceD.choice} </p> </Col>
</Row>
</Container>
)
}
}
class EndScreen extends React.Component {
render() {
if (this.props.currentQuestion === 7 && this.props.mostFrequent === "Warrior") {
return (
<Container fluid id = "end-screen">
<Row id = "result"> <h1> {endLabels[0].result} </h1> </Row>
<Row id = "result-picture"> <img src = {endLabels[0].imgSrc} /> </Row>
<Row id = "result-description"> <p> {endLabels[0].descrip} </p> </Row>
<Row id = "take-again" onclick = {this.props.reset}> <p> {endLabels[0].takeAgain} </p> </Row>
</Container>
)
}
if (this.props.currentQuestion === 7 && this.props.mostFrequent === "Rogue") {
return (
<Container fluid id = "end-screen">
<Row id = "result"> <h1> {endLabels[1].result} </h1> </Row>
<Row id = "result-picture"> <img src = {endLabels[1].imgSrc} /> </Row>
<Row id = "result-description"> <p> {endLabels[1].descrip} </p> </Row>
<Row id = "take-again" onclick = {this.props.reset}> <p> {endLabels[1].takeAgain} </p> </Row>
</Container>
)
}
if (this.props.currentQuestion === 7 && this.props.mostFrequent === "Sorcerer") {
return (
<Container fluid id = "end-screen">
<Row id = "result"> <h1> {endLabels[2].result} </h1> </Row>
<Row id = "result-picture"> <img src = {endLabels[2].imgSrc} /> </Row>
<Row id = "result-description"> <p> {endLabels[2].descrip} </p> </Row>
<Row id = "take-again" onclick = {this.props.reset}> <p> {endLabels[2].takeAgain} </p> </Row>
</Container>
)
}
if (this.props.currentQuestion === 7 && this.props.mostFrequent === "Bard") {
return (
<Container fluid id = "end-screen">
<Row id = "result"> <h1> {endLabels[3].result} </h1> </Row>
<Row id = "result-picture"> <img src = {endLabels[3].imgSrc} /> </Row>
<Row id = "result-description"> <p> {endLabels[3].descrip} </p> </Row>
<Row id = "take-again" onclick = {this.props.reset}> <p> {endLabels[3].takeAgain} </p> </Row>
</Container>
)
}
}
}
javascript-quiz-3.js (存储问题对象的位置):
// HTML fill-in for each part of quiz //
export const startLabels = [
{
title: "Which RPG class are you?",
imgSrc: "",
descrip: "Pick the lock, or break the face?",
startQuiz: "Start Quiz!",
},
];
export const questions = [
{
question: "Pick your weapon!",
imgSrc: "https://a.wattpad.com/cover/140215314-352-k843958.jpg",
choiceA: {
choice: "A stick I found on the ground.",
type: 'Warrior',
},
choiceB: {
choice: "I'll just steal if off someone.",
type: 'Rogue',
},
choiceC: {
choice: "Green stuff shooting out of my hands.",
type: 'Sorcerer',
},
choiceD: {
choice: "My silver tongue.",
type: 'Bard',
},
},
{
question: "There's an orc guarding the chest you need to get to. Do you...",
imgSrc: "https://a.wattpad.com/cover/140215314-352-k843958.jpg",
choiceA: {
choice: "Throw a rock to distract him and sneak in.",
type: 'Rogue',
},
choiceB: {
choice: "Smash their face in. And then the chest.",
type: 'Warrior',
},
choiceC: {
choice: "Convice the orc that you are their chief.",
type: 'Bard',
},
choiceD: {
choice: "Teleport the chest to you.",
type: 'Sorcerer',
},
},
{
question: "There's a strange mist washign over you. How do you respond?",
imgSrc: "https://a.wattpad.com/cover/140215314-352-k843958.jpg",
choiceA: {
choice: "Perceive what type of magic it is.",
type: 'Sorcerer',
},
choiceB: {
choice: "Use the mist to hide.",
type: 'Rogue',
},
choiceC: {
choice: "Ready your weapon and know you're going to use it.",
type: 'Warrior',
},
choiceD: {
choice: "Find the mist melancholic and beautiful. Get song ideas.",
type: 'Bard',
},
},
{
question: "You think you got a bad deal from a shopkeeper on a ring you sold. Do you... ?",
imgSrc: "https://a.wattpad.com/cover/140215314-352-k843958.jpg",
choiceA: {
choice: "Sneak back in when the shop is closed and take the ring.",
type: 'Rogue',
},
choiceB: {
choice: "Threaten the shop owner and note how you can smash their counter.",
type: 'Warrior'
},
choiceC: {
choice: "Cast an illusion in the shop that absorbs all metal objects in the shop until the ring is returned.",
type: 'Sorcerer'
},
choiceD: {
choice: "Charm the shopkeeper so they give you the ring back.",
type: 'Bard',
},
},
{
question: "You get ambushed on the way to the city. You are clearly outnumbered.",
imgSrc: "https://a.wattpad.com/cover/140215314-352-k843958.jpg",
choiceA: {
choice: "They may have numbers, but I have my axe.",
type: 'Warrior',
},
choiceB: {
choice: "Throw a smokebomb down and run to higher ground.",
type: 'Rogue',
},
choiceC: {
choice: "Create a ring of fire around you.",
type: 'Sorcerer',
},
choiceD: {
choice: "Play the ambush a song. Leave unaffected.",
type: 'Bard',
},
},
{
question: "What does your ideal world look like?",
imgSrc: "https://a.wattpad.com/cover/140215314-352-k843958.jpg",
choiceA: {
choice: "Everyone takes the time to learn.",
type: 'Sorcerer'
},
choiceB: {
choice: "Everyone would be honest and direct.",
type: 'Warrior',
},
choiceC: {
choice: "Endless beauty.",
type: 'Bard',
},
choiceD: {
choice: "Deeper pockets.",
type: 'Rogue',
},
},
{
question: "The front door is locked. What do you do?",
imgSrc: "https://a.wattpad.com/cover/140215314-352-k843958.jpg",
choiceA: {
choice: "Pick the lock of course!",
type: 'Rogue',
},
choiceB: {
choice: "Melt the lock.",
type: 'Sorcerer',
},
choiceC: {
choice: "It just needs more force!",
type: 'Warrior',
},
choiceD: {
choice: "Maybe find the key?",
type: 'Bard',
},
},
{
question: "Your quest is done. What do you do to unwind?",
imgSrc: "https://a.wattpad.com/cover/140215314-352-k843958.jpg",
choiceA: {
choice: "Flirt with the most beautiful people I can find.",
type: 'Bard',
},
choiceB: {
choice: "Explore the realm of each element.",
type: 'Sorcerer',
},
choiceC: {
choice: "Eat as much as I can!",
type: 'Warrior',
},
choiceD: {
choice: "Find a quiet corner to relax with my treasures.",
type: 'Rogue',
},
},
];
export const endLabels = [
{
result: "Warrior",
imgSrc: "",
descrip: "No problem can't be solved with my axe.",
takeAgain: "Take Again!"
},
{
result: "Rogue",
imgSrc: "",
descrip: "The shadows are my natural home.",
takeAgain: "Take Again!"
},
{
result: "Sorcerer",
imgSrc: "",
descrip: "Life, to you, is a world of mysteries to be uncovered.",
takeAgain: "Take Again!"
},
{
result: "Bard",
imgSrc: "",
descrip: "The world is boring without a little beauty.",
takeAgain: "Take Again!"
},
];
导航栏.js:
import React from 'react';
import {Navbar} from 'react-bootstrap';
import {Container} from 'react-bootstrap';
import {NavDropdown} from 'react-bootstrap';
import {Button} from 'react-bootstrap';
import {Link} from 'react-router-dom';
class TopNavbar extends React.Component {
render() {
return (
<div>
<Navbar expand = 'lg' bg = 'light' variant = 'light'>
<Container fluid>
<NavDropdown title = "|||" id = "basic-nav-dropdown">
<NavDropdown.Item> <Link to = "/"> Quizzes </Link> </NavDropdown.Item>
<NavDropdown.Item> <Link to = "/register"> Register </Link> </NavDropdown.Item>
<NavDropdown.Item> <Link to = "/subscribe"> Subscribe </Link> </NavDropdown.Item>
</NavDropdown>
<Link to = "/"> <img alt = 'placeholder'/> </Link>
<Link to = "/login"> <Button className = "login"> Login </Button></Link>
</Container>
</Navbar>
</div>
)
}
}
export default TopNavbar;
感谢任何可以提供帮助的人。我很感激。
最佳答案
我看到的两个问题:
reset()
您将 score
设置为 0
,它将没有 Array.prototype
方法。 Array.prototype.push
的返回值。它以 int 形式返回新数组的长度,而不是整个数组。如果您在任何地方将 this.state.score
分配给 push
的结果,即使只是一次,也会导致问题:https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/push 关于javascript - 为什么这个 React 状态数组不填充对象数据?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61375458/
初学者 android 问题。好的,我已经成功写入文件。例如。 //获取文件名 String filename = getResources().getString(R.string.filename
我已经将相同的图像保存到/data/data/mypackage/img/中,现在我想显示这个全屏,我曾尝试使用 ACTION_VIEW 来显示 android 标准程序,但它不是从/data/dat
我正在使用Xcode 9,Swift 4。 我正在尝试使用以下代码从URL在ImageView中显示图像: func getImageFromUrl(sourceUrl: String) -> UII
我的 Ubuntu 安装 genymotion 有问题。主要是我无法调试我的数据库,因为通过 eclipse 中的 DBMS 和 shell 中的 adb 我无法查看/data/文件夹的内容。没有显示
我正在尝试用 PHP 发布一些 JSON 数据。但是出了点问题。 这是我的 html -- {% for x in sets %}
我观察到两种方法的结果不同。为什么是这样?我知道 lm 上发生了什么,但无法弄清楚 tslm 上发生了什么。 > library(forecast) > set.seed(2) > tts lm(t
我不确定为什么会这样!我有一个由 spring data elasticsearch 和 spring data jpa 使用的类,但是当我尝试运行我的应用程序时出现错误。 Error creatin
在 this vega 图表,如果我下载并转换 flare-dependencies.json使用以下 jq 到 csv命令, jq -r '(map(keys) | add | unique) as
我正在提交一个项目,我必须在其中创建一个带有表的 mysql 数据库。一切都在我这边进行,所以我只想检查如何将我所有的压缩文件发送给使用不同计算机的人。基本上,我如何为另一台计算机创建我的数据库文件,
我有一个应用程序可以将文本文件写入内部存储。我想仔细看看我的电脑。 我运行了 Toast.makeText 来显示路径,它说:/数据/数据/我的包 但是当我转到 Android Studio 的 An
我喜欢使用 Genymotion 模拟器以如此出色的速度加载 Android。它有非常好的速度,但仍然有一些不稳定的性能。 如何从 Eclipse 中的文件资源管理器访问 Genymotion 模拟器
我需要更改 Silverlight 中文本框的格式。数据通过 MVVM 绑定(bind)。 例如,有一个 int 属性,我将 1 添加到 setter 中的值并调用 OnPropertyChanged
我想向 Youtube Data API 提出请求,但我不需要访问任何用户信息。我只想浏览公共(public)视频并根据搜索词显示视频。 我可以在未经授权的情况下这样做吗? 最佳答案 YouTube
我已经设置了一个 Twilio 应用程序,我想向人们发送更新,但我不想回复单个文本。我只是想让他们在有问题时打电话。我一切正常,但我想在发送文本时显示传入文本,以确保我不会错过任何问题。我正在使用 p
我有一个带有表单的网站(目前它是纯 HTML,但我们正在切换到 JQuery)。流程是这样的: 接受用户的输入 --- 5 个整数 通过 REST 调用网络服务 在服务器端运行一些计算...并生成一个
假设我们有一个名为 configuration.js 的文件,当我们查看内部时,我们会看到: 'use strict'; var profile = { "project": "%Projec
这部分是对 Previous Question 的扩展我的: 我现在可以从我的 CI Controller 成功返回 JSON 数据,它返回: {"results":[{"id":"1","Sourc
有什么有效的方法可以删除 ios 中 CBL 的所有文档存储?我对此有疑问,或者,如果有人知道如何从本质上使该应用程序像刚刚安装一样,那也会非常有帮助。我们正在努力确保我们的注销实际上将应用程序设置为
我有一个 Rails 应用程序,它与其他 Rails 应用程序通信以进行数据插入。我使用 jQuery $.post 方法进行数据插入。对于插入,我的其他 Rails 应用程序显示 200 OK。但在
我正在为服务于发布请求的 API 调用运行单元测试。我正在传递请求正文,并且必须将响应作为帐户数据返回。但我只收到断言错误 注意:数据是从 Azure 中获取的 spec.js const accou
我是一名优秀的程序员,十分优秀!