Java (IPYNB)
const factorial = function fac(n) { // calls a function which performs a calculation and returns the result
return n < 2 ? 1 : n * fac(n - 1);
}
console.log(factorial(7))
function printInfo(msg) { // Necessary definition
console.log(typeof msg + ";", msg);
}
function Media(name, cre, date) { // Define media
this.name = name;
this.cre = cre;
this.date = date;
this.type = "";
}
Media.prototype.setType = function(type) { // Introduce prototype
this.type = type;
}
Media.prototype.toJSON = function() { // organize
const data = {name: this.name, cre: this.cre, date: this.date, type: this.type};
const jsonString = JSON.stringify(data);
return jsonString;
}
var album = new Media("Kid A", "Radiohead", 2000); // song variable
album.setType("Album");
var movie = [ // movie variables
new Media("Whiplash", "Damien Chazelle", 2014),
new Media("Atonement", "Joe Wright", 2007),
new Media("Prestige", "Christopher Nolan", 2006)
]
var show = [ // show variables
new Media("Fargo", "Noah Hawley", 2014),
new Media("Daredevil", "Kati Johnson", 2015),
]
function Medias(album, movie, show) {
album.setType("Album");
this.album = album;
this.medias = [album];
this.movie = movie;
this.show = show;
this.movie.forEach(movie => {movie.setType("Movie"); this.medias.push(movie);});
this.show.forEach(show => {show.setType("Show"); this.medias.push(show);});
this.jsonMedias = [];
this.medias.forEach(media => this.jsonMedias.push(media.toJSON()));
}
catalog = new Medias(album, movie, show);
printInfo(catalog.medias);
printInfo(catalog.medias[4].name);
printInfo(catalog.jsonMedias[4]);
printInfo(JSON.parse(catalog.jsonMedias[4]));