Q:

How to fix "Invalid email undefined" error in closing MailApp.sendemail step

For a new course project, I'm coming back to a google script for SendEmail from Google Sheets that worked perfectly a couple of years ago. (Original code from https://medium.com/@sebfung/custom-emails-from-a-spreadsheet-b11ff050bc5f ). Now I get three errors and can't figure out how to fix the third.
The full code is below. I've got limited code experience, but I figure I can work around the first two errors I get because they're cosmetic and my students can deal with it. (First, I can take out code to live without the "split" that used to let me drop off the second half of email addresses in the SendToName salutation. Second, I can take out the "replace" code that switches newline to paragraph breaks.) 
But I can't figure out how to get around the third error. Something in the last command (MailApp.SendEmail) gives me a "Invalid email: undefined (line 66, file "Code")" error.
I don't see that any of these could be a spreadsheet-related error, since I went back to the earlier spreadsheets that worked a couple years ago and the debug now brings up the same three errors.
I'd be happy for suggestions on the first two, but I really most need and value help with that third error. I can't even run or test anything unless I fix it. Thanks!
function sendEmail(to, score, questions, answer1, answer2, answer3, answer4, answer5) {
  var ss = SpreadsheetApp.getActiveSheet();
  var range = ss.getActiveRange(); 

  var numRows = range.getNumRows();
  var values = range.getValues();

  for (var i = 0; i <= numRows - 1; i++) {
    var row = values[i];
    var to = row[1];
    var score = row[2];
    var questions = row[3]
    var answer1 = row[4];
    var answer2 = row[5];
    var answer3 = row[6];
    var answer4 = row[7];
    var answer5 = row[8];

    var sendToName = to.split('@')[0];

  // email content
  var emailSubject = "Quiz 1 Response";

  // plain text body - just in case receiver can't parse html formatted email
  var plainTxtBody = "Hello " + sendToName + ",\n" +
                 "You scored:\n" + score + "\n" +
                 "The questions were:\n" + questions + "\n" +
                 "Your answer:\n" + answer1 + "\n" +
                 "Your answer:\n" + answer2 + "\n" +
                 "Your answer:\n" + answer3 + "\n" +
                 "Your answer:\n" + answer4 + "\n" +
                 "Your answer:\n" + answer5; 

  // html formatting isn't necessary, but nice for reading :)
  var htmlBody = '<html><body>';
  var htmlFooter = '</body></html>';

  // replacing newline characters with paragraph breaks to make it more readable
  var scoreHTML = score.replace(/\n/g, "</p><p>");
  var questionsHTML = questions.replace(/\n/g, "</p><p>");  
  var answer1HTML = answer1.replace(/\n/g, "</p><p>");
  var answer2HTML = answer2.replace(/\n/g, "</p><p>");
  var answer3HTML = answer3.replace(/\n/g, "</p><p>");
  var answer4HTML = answer4.replace(/\n/g, "</p><p>");
  var answer5HTML = answer5.replace(/\n/g, "</p><p>");

  var emailMessage = "<p>Hello " + sendToName + ",</p>" +
                 "<p><strong>You scored:</strong><p>" +
                 "<p>" + scoreHTML + "</p>" +
                 "<hr>" + 
                 "<p><strong>Here are the questions--see the answer key on the Quiz 1 page on the wiki:</strong></p>" + 
                 "<p>" + questionsHTML + "</p>" +
                 "<hr>" +
                 "<p><strong>Here are your answers:</strong></p>" + 
                 "<p>" + answer1HTML + "</p>" +
                 "<p>" + answer2HTML + "</p>" +
                 "<p>" + answer3HTML + "</p>" +
                 "<p>" + answer4HTML + "</p>" +
                 "<p>" + answer5HTML + "</p>";

  htmlBody += emailMessage + htmlFooter;

  // GmailApp must have default recipient, subject, body attributes followed by jsobject options {}
  // Differs from MailApp syntax but has more options -- see documentation on GmailApp
  MailApp.sendEmail(to, emailSubject, plainTxtBody, { 
                 htmlBody: htmlBody,
                    });
   }
}

A:

The script does work. Var "to" probably doesn't contain a valid email adres. Make sure you have selected an active range in your spreadsheet. Please also note the first item in an array has index 0. Which reflects to the first column in your selection. 
You can set a break point and use the debugger or use Logger.log(to); View -> Logs will show the outcome of var to.

