我设置了 3 个复选框(未开始、进行中、已完成),我希望在某个时间只允许选中一个。
因此,如果一开始就自动检查“未开始”,那么如果我检查“已完成”,如何使其取消选中“未开始”?
现在这是我的代码:
在 App.js 中:
const newGame = {
id: uuid.v4(),
title: title,
hours: hours,
notStarted: true,
inProgress: false,
completed: false,
}
notStarted = (id) => {
this.setState({games: this.state.games.map(game => {
if(game.id === id){
game.notStarted = !game.notStarted
game.inProgress = false;
game.completed = false;
}
return game;
})})
};
markCompleted = (id) => {
this.setState({games: this.state.games.map(game => {
if(game.id === id){
game.completed = !game.completed
game.notStarted = false;
game.inProgress = false;
}
return game;
})})
};
Run Code Online (Sandbox Code Playgroud)
在 render() 方法中: …
所以我想为我的类写一个函数,它的标题isAlpha是接受一个字符(最好是一个长度为1的字符串),如果是一个字母则返回true,如果不是则返回false.
问题是我完全被困在哪里去了.这是教师在课堂上给出的例子:
var isAlpha = function(ch){
//if ch is greater than or equal to "a" AND
// ch is less than or equal to "z" then it is alphabetic
}
var ltr ="a", digit =7;
alert(isAlpha(ltr));
alert(isAlpha(digit))
Run Code Online (Sandbox Code Playgroud)
我不知道该怎么办,我尝试了一些不同的东西,比如:
var isAlpha = function(ch){
if (ch >= "A" && ch <= "z"){
return true
}
}
alert(isAlpha(ch))
Run Code Online (Sandbox Code Playgroud)
任何人都能指出我如何开始这个功能的正确方向?
好吧,我似乎无法让这个工作.到目前为止我写了一个函数.它很棒.现在我需要让我的新函数调用第二个函数.
这是第一个功能:
function isVowelR(str) {
if(str = str.match(/[aeiou]/gi))
return true
else
return false
}
Run Code Online (Sandbox Code Playgroud)
这个工作正常.如果str是元音,则返回true.这是我坚持的一个.这是我尝试过的(以及其他一些东西.
function countVowels(str) {
var count = 0
for (var i = 0; i == str.length; i++)
{
if (i == isVowelR(i))
{
++count
}
}
return count
}
Run Code Online (Sandbox Code Playgroud)
第二个函数需要计算输入的字符串中有多少个元音(我在不同的函数中计算出来).但是如果我需要调用第一个函数,如何让它工作?