Implement the following user story:
It’s done when the total number of elements in the array is logged to the console.
It’s done when the message “Welcome to the class STUDENT_NAME” is logged using each element in the array.
It’s done when the first element in the array is replaced with the name of a new student.
It’s done when, after an if
statement confirms that the first element in the array has been replaced, the message “REPLACED_NAME is in class” is logged.
What is the first index in an array: 0
or 1
?
If you have completed this activity, work through the following challenge to further your knowledge:
length
property to access the last element in an array of any length?Use Google or another search engine to research this.
// Creates an array containing names of five students in the class
var students = ["Sarah", "Orlando", "Heather", "Ismael", "Hung"];
// Logs length of the students array
console.log(students.length);
// Now, write a console log introducing each student
console.log("Welcome to the class " + students[0]);
console.log("Welcome to the class " + students[1]);
console.log("Welcome to the class " + students[2]);
console.log("Welcome to the class " + students[3]);
console.log("Welcome to the class " + students[4]);
// Replace the first student in the array with a new student Bob.
students[0] = "Bob";
// Use your Javascript to check if "Bob" is the first elemnt in the array
if (students[0] === "Bob") {
console.log(students[0] + " is in class!");
}