天気を評価しようとしています。ユーザー入力文字列内の単語はすべて「@」で始まります。javascriptの配列の各文字列の最初の文字を見てください
Iveは、入力文字列を単語の配列に分割して開始しました。 その後、最初の文字を見て各単語をループし、 "@"があるかどうかを確認します。
charAt()がエラーを返すように見える理由を理解できません。
<!DOCTYPE html>
<html>
<body>
<input id="txt" type="textarea" />
<button onclick="myFunction()">Try it</button>
<script>
function myFunction(){
var text = document.getElementById('txt').value;
var inputArray = [text.split(" ")];
console.log(inputArray);
for(i=0; i < inputArray.length; i++){
console.log(inputArray[i]);
if(inputArray[i].charAt(0) === '@'){
console.log("its an at symbol");
} else{
console.log("nope");
}
}
}
</script>
</body>
</html>
'text.split(" "") 'はすでに配列を返します。なぜあなたはそれを1つに包んでいますか? – Xufox
'const symbols = text.match(/ @ \ w + /)|| []; ' –