• Recent
  • Tags
  • Unsolved
  • Solved
  • MagicMirror² Repository
  • Documentation
  • 3rd-Party-Modules
  • Donate
  • Discord
  • Register
  • Login
MagicMirror Forum
  • Recent
  • Tags
  • Unsolved
  • Solved
  • MagicMirror² Repository
  • Documentation
  • 3rd-Party-Modules
  • Donate
  • Discord
  • Register
  • Login
A New Chapter for MagicMirror: The Community Takes the Lead
Read the statement by Michael Teeuw here.

Dinner Shuffler - Randomly select dinner for every day of the week

Scheduled Pinned Locked Moved Development
2 Posts 2 Posters 1.9k Views 3 Watching
Loading More Posts
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • E Offline
    espenrog94
    last edited by paviro Sep 27, 2016, 8:54 PM Aug 17, 2016, 7:28 PM

    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!

    1 Reply Last reply Reply Quote 0
    • P Offline
      paviro Admin
      last edited by Sep 27, 2016, 8:55 PM

      Why not upload it to GitHub or GitLab and then present it in the module showcase section? :)

      We're all stories in the end. Just make it a good one, eh?

      – The Doctor

      1 Reply Last reply Reply Quote 0
      • 1 / 1
      • First post
        Last post
      Enjoying MagicMirror? Please consider a donation!
      MagicMirror created by Michael Teeuw.
      Forum managed by Sam, technical setup by Karsten.
      This forum is using NodeBB as its core | Contributors
      Contact | Privacy Policy