Hi,
Made a module for displaying a random list of dinners for every day of the week because I’m sick of thinking of what to have for dinner every day. I’m very new to javascript so there might be several bugs and issues that I’m not aware of. Apologies for that and also for not using module config and defaults in the code.
Couple of things that could be improved with the help of your guidance :)
- Stop function from being run every time MM is restarted or refreshed. Want to keep the list of dinners for a week.
- Have the function run once a week at a set time. For instance Sunday at 6 pm (or maybe by button push)
- Saturday and Sunday dinners should be from a different pick than the rest of the week. Want to eat less healthy on weekends :)
- Have line breaks between elements of an array without using “textarea” and .join(“\n”)
Anyway, the code is pasted below. Hope you find it useful!
/* global Module */
/* Magic Mirror
* Module: DinnerShuffler
*
* By Michael Teeuw http://michaelteeuw.nl
* MIT Licensed.
*/
Module.register("dinnershuffler",{
// Module config defaults.
defaults: {
updateInterval: 7 * 24 * 60 * 60 * 1000
//fadeSpeed: 4000
},
// Define required scripts.
getScripts: function() {
return ["moment.js"];
},
// Define start sequence.
start: function() {
Log.info("Starting module: " + this.name);
//Schedule update timer.
var self = this;
setInterval(function() {
self.updateDom(self.config.fadeSpeed);
}, this.config.updateInterval);
},
/**
* Randomize array element order in-place.
* Using Durstenfeld shuffle algorithm.
*/
shuffle: function Shufflearray(array) {
for (var i = array.length - 1; i > 0; i--) {
var j = Math.floor(Math.random() * (i + 1));
var temp = array[i];
array[i] = array[j];
array[j] = temp;
}
return array;
},
/* dinnerArray()
* Retrieve a randomly shuffled array of dinners including 2 with fish
*
* return list of dinners for a week
*/
dinnerArray: function() {
var dinners = [
'Dinner1',
'Dinner2',
'Dinner3',
'Dinner4',
'Dinner5',
'Dinner6',
'Dinner7',
'Dinner8',
'Dinner9',
'Dinner10'
];
var fish = [
'Fish1',
'Fish2',
'Fish3',
'Fish4',
'Fish5',
'Fish6',
'Fish7'
];
var dayofweek = [
'Monday : ',
'Tuesday : ',
'Wednesday : ',
'Thursday : ',
'Friday : ',
'Saturday : ',
'Sunday : '
]
//Shuffle the dinners array
var dinnersShuffled = this.shuffle(dinners);
//Shuffle the fish array
var fishShuffled = this.shuffle(fish);
//Make new array with both dinners and fish
var all = [];
for (i = 0; i < 5; i++) {
all.push(dinnersShuffled[i]);
}
for (i = 0; i < 2; i++){
all.push(fishShuffled[i]);
}
//Shuffle the "all" array that contains both dinners and fish
var allShuffled = this.shuffle(all);
//Make a new array that includes day of week before every position of the allSHuffled array
var allShuffledDay =[];
for (i = 0; i < 7; i++){
allShuffledDay.push([dayofweek[i] + allShuffled[i]]);
}
//Return list of dinners for every day of the week. Using .join("\n") to get line break between each element of the array. NB! Must use "textarea" in documnet.createElement (see getDom under) to get line break.
return allShuffledDay.join("\n");
},
// Override dom generator.
getDom: function() {
var wrapper = document.createElement("textarea");
wrapper.style.width = "350px";
wrapper.style.height = "200px";
wrapper.style.background = "black";
wrapper.style.color = "white";
wrapper.style.border = "transparent";
wrapper.style.resize = "none";
wrapper.style.fontSize = "x-large";
wrapper.style.font = "roboto-thin";
wrapper.innerHTML = this.dinnerArray();
return wrapper;
}
});
Note from admin: Please use Markdown on code snippets for easier reading!