Value1.length is sending a undefined error however the other two values are not. Why is it only the first value isn't running properly?
placeOrder.addEventListener("click", function (e) {
e.preventDefault();
var value1 = input1.value;
if (value1.length < 1) {
value1 = 0;
coffeeTotal = 0;
} else {
coffeeTotal = coffeePrice * parseInt(value1);
}
var value2 = input2.value;
if (value2.length < 1) {
value2 = 0;
bagelTotal = 0;
} else {
bagelTotal = bagelPrice * parseInt(value2);
}
var value3 = input3.value;
if (value3.length < 1) {
value3 = 0;
biscottiTotal = 0;
} else {
biscottiTotal = biscottiPrice * parseInt(value3);
}
var total = coffeeTotal + bagelTotal + biscottiTotal;
console.log(total);
});
What does typeerror: cannot read property “values” from undefined really mean?
To understand this, you first need to understand what it means to be undefined in JavaScript. Let’s take a look at a simple example using variables:
// This line is a variable declaration
// we create it, but do not assign it a value
// so it is undefined
var a;
// In this line we declare and assign a variable
// this is not undefined
var b = 6;
Something is undefined when it is an object that is expected to hold a reference to a value, but in fact references nothing. So you have to check your input1's value.