var log = function(content){ $('<p/>').html(content).appendTo('#log'); } function doTask1(){ var rslt = Math.floor(Math.random() * 10); log("Task 1 returned " + rslt); return rslt; } function addTask(i){ var rslt = Math.floor(Math.random() * 10); log("addTask finished with " + rslt); return i + rslt; } function multiplyTask(i){ var rslt = Math.floor(Math.random() * 10); log("multiplyTask finished with " + rslt); return i * rslt; } $(function(){ var task1 = doTask1(); var finish; if(task1 >= 5) finish = addTask(task1); else finish = multiplyTask(task1); log("All tasks finished with " + finish); });
<div id="log"></id>