Work with a partner to resolve the following issue(s):
When a user runs the script, it will add and remove movies from the queue and display the movies currently in the queue.
The script exits out with an error message.
Navigate to the starter
folder from the command line.
Run node index.js
.
Note the error that is printed.
What is an example of when we shouldn’t use arrow functions?
If you have completed this activity, work through the following challenge with your partner to further your knowledge:
Use Google or another search engine to research this.
// 1. funnyCase makes each letter in a string the opposite case as the one before
var funnyCase = string => {
var newString = "";
for (var i = 0; i < string.length; i++) {
if (i % 2 === 0) newString += string[i].toLowerCase();
else newString += string[i].toUpperCase();
}
return newString;
};
// Prints `yOu cAn't jUsT Do wHaTeVeR YoU WaNt aLl tHe tImE!`
console.log(funnyCase("You can't just do whatever you want all the time!"));
// --------------------------------------------------------------------------
// 2. Map lets you loop over an array and modify the elements inside
var map = (arr, cb) => {
var result = [];
for (var index = 0; index < arr.length; index++) {
var currentElement = arr[index];
result.push(cb(currentElement, index));
}
return result;
};
var numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
var doubled = map(numbers, element => element * 2);
// Prints `[ 2, 4, 6, 8, 10, 12, 14, 16, 18, 20 ]`
console.log(doubled);
// --------------------------------------------------------------------------
// 3. filter lets you loop over an array and remove elements
var filter = (arr, cb) => {
var result = [];
for (var index = 0; index < arr.length; index++) {
var currentElement = arr[index];
if (cb(currentElement, index)) {
result.push(currentElement);
}
}
return result;
};
var evenNumbers = filter(numbers, currentElement => currentElement % 2 === 0);
// Prints `[ 2, 4, 6, 8, 10 ]`
console.log(evenNumbers);
// --------------------------------------------------------------------------
// 4. netflixQueue is an object for managing your netflix queue
var netflixQueue = {
queue: [
"Mr. Nobody",
"The Matrix",
"Eternal Sunshine of the Spotless Mind",
"Fight Club"
],
watchMovie: function() {
this.queue.pop();
},
addMovie: function(movie) {
this.queue.unshift(movie);
},
printQueue: function() {
var list = "";
for (var i = this.queue.length - 1; i >= 0; i--) {
var currentMovie = this.queue[i];
list += (this.queue.length - i) + ". " + currentMovie + "\n";
}
console.log(list);
}
};
console.log("Printing movie queue!\n");
netflixQueue.printQueue();
netflixQueue.watchMovie();
console.log("\nWatched a movie!\n");
console.log("Printing movie queue!\n");
netflixQueue.printQueue();
console.log("\nAdding a movie!\n");
netflixQueue.addMovie("Black Swan");
console.log("Printing movie queue!\n");
netflixQueue.printQueue();