admin管理员组文章数量:1326112
I have my code:
var name = [];
var mark1 = [];
var mark2 = [];
var mark3 = [];
var total = [];
count = 0
count2 = 0
var i = 0;
while (count != 2) {
var nam = prompt("Enter name:")
name.push(nam);
var mk1 = prompt("Enter mark 1:");
var mk1 = parseInt(mk1);
mark1.push(mk1);
var mk2 = prompt("Enter mark 2:");
var mk2 = parseInt(mk2);
mark2.push(mk2);
var mk3 = prompt("Enter mark 2:");
var mk3 = parseInt(mk3);
mark3.push(mk3);
var tot = mk1 + mk2 + mk3;
total.push(tot)
count = count + 1
console.log(mk1 + mk2 + mk3);
console.log(nam);
console.log("the count is " + count)
};
When I run it I get an error:
Uncaught TypeError: undefined is not a function
on Line 12 which is name.push(nam);
I have looked around but I am not sure what I am doing wrong. Help appreciated.
I have my code:
var name = [];
var mark1 = [];
var mark2 = [];
var mark3 = [];
var total = [];
count = 0
count2 = 0
var i = 0;
while (count != 2) {
var nam = prompt("Enter name:")
name.push(nam);
var mk1 = prompt("Enter mark 1:");
var mk1 = parseInt(mk1);
mark1.push(mk1);
var mk2 = prompt("Enter mark 2:");
var mk2 = parseInt(mk2);
mark2.push(mk2);
var mk3 = prompt("Enter mark 2:");
var mk3 = parseInt(mk3);
mark3.push(mk3);
var tot = mk1 + mk2 + mk3;
total.push(tot)
count = count + 1
console.log(mk1 + mk2 + mk3);
console.log(nam);
console.log("the count is " + count)
};
When I run it I get an error:
Uncaught TypeError: undefined is not a function
on Line 12 which is name.push(nam);
I have looked around but I am not sure what I am doing wrong. Help appreciated.
Share Improve this question edited Nov 23, 2014 at 18:38 CD.. 74.2k25 gold badges159 silver badges169 bronze badges asked Nov 23, 2014 at 18:32 crablabcrablab 251 gold badge3 silver badges9 bronze badges 6- I can't reproduce that error with your code. – Quentin Commented Nov 23, 2014 at 18:34
- one important thing when asking for help is to make sure your code's properly indented. Humans like to be able to read things easily. Another is to confirm that's really what's going wrong. If we drop this code into jsbin. or jsfiddle, it works just fine – Mike 'Pomax' Kamermans Commented Nov 23, 2014 at 18:35
- no errors thrown here: jsfiddle/6yjwh1a5 – BeNdErR Commented Nov 23, 2014 at 18:35
-
1
@BeNdErR your link wraps the code in a function, protecting the identifier conflict of the
var name
withwindow.name
– Paul S. Commented Nov 23, 2014 at 18:40 - May be you have skipped this line var name = [];, because of this line, this type of error is generated. – Varun Chakervarti Commented Nov 23, 2014 at 18:41
1 Answer
Reset to default 9This is an interesting one. It all boils down to an unfortunate choice of variable name. Unfortunately name
is a property of the window
object. When you refer to name
you are actually referring to window.name, not the array called name
. If you rename name
to something else, it should work just fine.
本文标签: Javascript arraypush is undefinedStack Overflow
版权声明:本文标题:Javascript: array.push is undefined - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742192433a2430462.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论