Here’s my latest error from 3 different students - ERROR: Line: 32: TypeError: contacts[currentIndex] is undefined
var currentIndex = 0;
var contacts = ;
readRecords(“myContacts”, {}, function(records){
contacts = records;
showCurrentContact();
});
showCurrentContact();
onEvent(“viewContactsScreen”, “keydown”, function(event) {
if(event.key == “Left”){
currentIndex–;
currentIndex = wrap(currentIndex, 0, contacts.length-1);
showCurrentContact();
} else if (event.key == “Right”){
currentIndex++;
currentIndex = wrap(currentIndex, 0, contacts.length-1);
showCurrentContact();
}
});
onEvent(“addContactBtn”, “click”, function() {
setScreen(“addContactsScreen”);
});
onEvent(“backBtn”, “click”, function() {
setScreen(“viewContactsScreen”);
});
function showCurrentContact(){
var EntireName = "Name: " + (contacts[currentIndex].name);
var FullPhone = "Phone: " + (contacts[currentIndex].phone);
var FullBirthday = "Birthday: " + (contacts[currentIndex].birthday);
setText(“contactInfo”, (EntireName + ‘\n’) + FullPhone + ‘\n’ + FullBirthday);
setImageURL(“contactImage”, (contacts[currentIndex].imageURL));
}
function wrap(val, low, high){
var output;
if(val < low){
output = high;
} else if (val > high){
output = low;
} else {
output = val;
}
return output;
}
onEvent(“saveContactBtn”, “click”, function() {
var newerContact = {};
newerContact.name = getText(“nameInput”);
newerContact.phone = getText(“phoneInput”);
newerContact.birthday = getText(“birthdayInput”);
newerContact.imageURL = getText(“URLinput”);
appendItem(contacts, newerContact);
setText(“nameInput”, “”);
setText(“phoneInput”, “”);
setText(“birthdayInput”, “”);
setText(“URLinput”, “”);
currentIndex = contacts.length - 1;
showCurrentContact();
setScreen(“viewContactsScreen”);
createRecord(“myContacts”, newerContact, function(){
});
});
onEvent(“URLinput”, “input”, function() {
setImageURL(“previewImage”, getText(“URLinput”));
console.log("URLinput entered text: " + getText(“URLinput”));
});