当我在数组中输入数字时,会收到两个警报:"number"
警报,然后是"undefined"
警报。我不知道如何解决这个问题。
这是我的JavaScript代码:
var myStuff = [];
function myfunctionA() {
var enteredvalue = document.getElementById("numbers").value;
alert(typeof Number(document.getElementById('numbers').value));
if (enteredvalue == "") {
alert("Input is not a number");
} else if (isNaN(enteredvalue)) {
alert('You need to enter a valid number!');
}
var elementExists = false;
var x = document.getElementById('numbers').value;
for (var i = 0; i < myStuff.length; i++) {
if (myStuff[i] == Number(x)) {
elementExists = true;
}
}
if (elementExists != true) {
myStuff.push(Number(enteredvalue));
alert('Thank You for entering a valid number'. myStuff);
} else {
alert('Element is here');
}
}
function myfunctionB() {
window.alert(myStuff.length);
}
function myfunctionC() {
var sum = 0;
for (var i = 0; i < myStuff.length; i++) {
sum += myStuff[i];
}
alert(sum);
}
function myfunctionD() {
if (myStuff.length == 0) {
alert("already empty");
} else {
myStuff = [];
}
alert("Array Empty");
}
function myfunctionE() {
alert(myStuff.join('\n')); {
if (myStuff == [])
{
alert("Enter something into Array")
}
}
}
function bubbleSort() {
var sorted = true;
var temp;
while (sorted) {
sorted = false;
for (var i = 0; i < myStuff.length - 1; i++) {
if (myStuff[i] < myStuff[i + 1]) {
temp = myStuff[i];
myStuff[i] = myStuff[i + 1];
myStuff[i + 1] = temp;
sorted = true;
}
}
}
}
答案 0 :(得分:1)
以下行评估为未定义:
alert('Thank You for entering a valid number'. myStuff);
你可能意思是?:
alert('Thank You for entering a valid number' + myStuff);