[英]How can I check the value of this.state with an if statement and not console.log()?
[英]How Can I check if both Arrays inside this.state contains same value?
我有兩個數組,其中一個包含正確答案的索引號correctAnswers
並且userAnswers
包含用戶選擇的答案的索引號
如何檢查兩個索引的值是否相同,如果是,則將+1加到Total
this.state = {
correctAnswers: [], // [1, 2, 3, 1, 2, 1, 3, 3, 4, 3]
userAnswers: [], // [1, 3, 1, 2, 2, 1, 1, 3, 4, 3]
Total: 0
};
因此,在上述情況下,Total應該為Total =6
我的嘗試:
const correctAnswers = this.state.correctAnswers;
const userAnswers = this.state.userAnswers;
compareAnswers() {
for (var i = 0; i < correctAnswers.length; i++) {
if (correctAnswers[i] === userAnswers[i]) {
this.setState({ Total: +1 });
} else console.log("ITS NOT A RIGHT ANSWER ");
}
}
好的,因為它是對象內部的數組,並且實際對象具有this
引用,所以我們必須容納this
引用。
for(var i=0;i<this.state.correctAnswers.length;i++){
if(this.state.correctAnswers[i] === this.state.userAnswers[i]){
this.state.Total++;
}
}
使用此方法,我們可以動態比較兩個數組。 希望這可以幫助!
您的嘗試看起來不錯,除了它有語法錯誤(您不能這樣做Total: +1
)。 取而代之的是,您可以使用功能性的setState
語法,對於您的代碼段而言,語法類似於:
compareAnswers() {
for (var i = 0; i < correctAnswers.length; i++) {
if (correctAnswers[i] === userAnswers[i]) {
this.setState(oldState => ({...oldState, Total: oldState.Total++}));
} else console.log("ITS NOT A RIGHT ANSWER ");
}
}
但是,最好將所有的總和分批處理,並在完成后只設置一次狀態 ,如下所示:
compareAnswers() {
let newTotal = 0;
for (var i = 0; i < correctAnswers.length; i++) {
if (correctAnswers[i] === userAnswers[i]) {
newTotal++;
} else console.log("ITS NOT A RIGHT ANSWER ");
}
this.setState({Total: newTotal})
}
我的嘗試是基於所有問題都是強制性的,並且用戶會依次回答的假設:
compareAnswer() {
// Addressing the best case i.e. when all user answers are correct
if (JSON.stringify(this.state.correctAnswers) == JSON.stringify(this.state.userAnswers)) {
this.setState({ Total: this.state.correctAnswers.length });
} else {
for (var i=0; i<this.state.userAnswers.length; i++) {
if (this.state.correctAnswers[i] === this.state.userAnswers[i]) {
this.setState({ Total: this.state.Total++ });
}
}
}
}
將方法添加到您已經擁有的對象中:
let state = { correctAnswers: [1, 2, 3, 1, 2, 1, 3, 3, 4, 3], userAnswers: [1, 3, 1, 2, 2, 1, 1, 3, 4, 3], Total: 0, evalScore() { this.correctAnswers.forEach((answer, index) => { if (answer === this.userAnswers[index]) { this.Total++ } }) } } state.evalScore(); console.log(state.Total);
您甚至可以為此使用Total
屬性,並使它像計算屬性一樣工作:
let state = { correctAnswers: [1, 2, 3, 1, 2, 1, 3, 3, 4, 3], userAnswers: [1, 3, 1, 2, 2, 1, 1, 3, 4, 3], Total() { // make sure both array have the same number of elements if(this.correctAnswers.length !== this.userAnswers.length) { return null; } let total = 0 this.correctAnswers.forEach((answer, index) => { if (answer === this.userAnswers[index]) { total++ } }) return total } } console.log(state.Total());
您可以使用forEach
和三元條件:
state.Total += (c === state.correctAnswers[i] ? 1 : 0)
var state = { correctAnswers: [1, 2, 3, 1, 2, 1, 3, 3, 4, 3], userAnswers: [1, 3, 1, 2, 2, 1, 1, 3, 4, 3], Total: 0 }; state.userAnswers.forEach((c, i) => state.Total += (c === state.correctAnswers[i] ? 1 : 0)); console.log(state)
.as-console-wrapper { max-height: 100% !important; top: 0; }
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.