我有这段代码:
class CombatLog {
constructor(){
this.idMsg = 0;
this.timerInterval;
}
startTimer(){
this.timerInterval = setInterval(this.combatStartLog, 2000);
$('#combatLog').empty();
}
combatStartLog(){
console.log(this.idMsg);
switch (this.idMsg){
case 3:
clearInterval(this.timerInterval);
$('#combatLog').empty();
break;
case 2:
$('<p>', {
class: 'combatText',
id: `${this.idMsg}`
}).appendTo('#combatLog');
$(`#${this.idMsg}`).append(`FIGHT!`);
this.idMsg = 3;
break;
case 1:
$('<p>', {
class: 'combatText',
id: `${this.idMsg}`
}).appendTo('#combatLog');
$(`#${this.idMsg}`).append(`Prepare your potions...`);
this.idMsg = 2;
break;
case 0:
$('<p>', {
class: 'combatText',
id: `${this.idMsg}`
}).appendTo('#combatLog');
$(`#${this.idMsg}`).append(`Unsheathe your weapons...`);
this.idMsg = 1;
break;
default:
this.idMsg = 0;
}
}
Run Code Online (Sandbox Code Playgroud)
所需的行为将是:
我有一个加载器,它在传递字符串时显示加载消息,但在传递数组时我想通过消息数组循环显示多个消息。
const messages = ['fetching from sources...', 'loading account...'];
<Loader message={messages}/>
const Loader = (Props) => {
const { message } = props;
const renderMessages = (msgs) => {
console.log(msgs);
return msgs.forEach((msg, i) => {
setTimeout(() => {
return <Message>{msg}</Message>;
}, 500);
});
};
return (
<LoaderContainer>
<LoaderSvg width="120" height="120" viewBox="0 0 100 100" />
{(Array.isArray(message)) ? renderMessages(message) : <Message>{message}</Message>}
</LoaderContainer>
);
};
Run Code Online (Sandbox Code Playgroud) 根据MDN
如果参数不能转换为数字,则返回
NaN
有些人可以解释原因Number('')并Number(null)返回,0因为没有有效的数字。
首先我认为这可能是因为它们是错误的值。但后来我发现Number(undefined)不返回0。
console.log(Number(undefined)); //NaN
console.log(Number(null)); //0
console.log(Number('')); //0Run Code Online (Sandbox Code Playgroud)
该程序只是有一个函数来检查一个数字是否是一个完美的数字。完美是其所有因素(不包括数字本身)的总和等于数字本身。6(1 + 2 + 3 = 6)也喜欢28
我看到了一种奇怪的行为。有cout << ""。如果我们删除这一行代码将无法正常工作,如果我们把它一切正常。我无法理解这个
#include<iostream>
using namespace std;
int IsPerfect(int);
int main(){
int N, j;
cout << "Enter a value for N: ";
cin >> N;
cout << "Perfect numbers are ";
for(j=1; j<=N; j++){
cout << ""; //If you remove this line no output will be printed
if(IsPerfect(j) == 1){
cout << j << endl;
}
}
}
int IsPerfect(int a){
int sum=0;
for(int i; i<a; i++){
if(a%i …Run Code Online (Sandbox Code Playgroud)