admin管理员组

文章数量:1325374

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 with window.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
 |  Show 1 more ment

1 Answer 1

Reset to default 9

This 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