Placid Posted August 24, 2012 Share Posted August 24, 2012 Dear Masters: I am new to JS, learning it first from this book. Completed sequentially upto Ch 6. Now I am stuck at Chapter 6, Pursue Problem No. 3 - "Update the original tasks.js so that the output also shows a random task." I have tried many ways but unable to generate a purely random task. Can any one help, please. I am using: Browser: Firefox 13.01 OS: Windows 7 IDE: Aptana Studio 3 P.S. My gratitudes to Sir Ullman for this awesome book. I have tried many but started to really learn from here. Thank you. Placid Link to comment Share on other sites More sharing options...
HartleySan Posted August 24, 2012 Share Posted August 24, 2012 All the tasks are in an array named tasks. To get a random one, you need to get a random number, resize that relative to the size of the array, and then output that random task in addition to the message already being output. Just to give you some hints (without giving away the whole solution): 1) Use Math.random() to generate a random number. Math.random() generate a random decimal between 0 and 1 (non-inclusive, I think). 2) To make that random number returned by Math.random() relevant for getting a random task, multiple that random number by the length of the tasks array. You will also have to do some rounding to get an integer that corresponds to a valid index in the tasks array. 3) Add some text onto the end of the message string variable before outputting it to the screen. For example, add on, 'And here's a random task: ' + randomly-picked-task. Hope that helps. 2 Link to comment Share on other sites More sharing options...
Placid Posted August 25, 2012 Author Share Posted August 25, 2012 It worked !! Thank you so much. The part of your solution that I needed was: "multiple that random number by the length of the tasks array". Thank you again. I really appreciate. Link to comment Share on other sites More sharing options...
HartleySan Posted August 25, 2012 Share Posted August 25, 2012 No prob. I had a feeling that you were just missing one little piece and had the rest down, so I didn't want to give you too much help. Good work! Link to comment Share on other sites More sharing options...
markifornia Posted September 4, 2012 Share Posted September 4, 2012 Hi placid, I was stuck where you were at and tried the below, finally got it to work. var randomNumber = tasks.length; var randomTask = parseInt(Math.random() * randomNumber, 10) + ''; message += '<li>Random Task:' + tasks[randomTask] + '</li>'; Is this similar to yours? Also have you figured out how to modify the script so that the join() function is used to create the final message (instead of concatenating them)? message = '<h2>To-Do</h2><ol><li>'; for (var i = 0, count = tasks.length; i < count; i++) { message += tasks.join('<li><li>'); task.value = ""; } message += '</li></ol>'; The code above keeps creating an empty list elements, each time an element is added / loop is iterated. Link to comment Share on other sites More sharing options...
HartleySan Posted September 4, 2012 Share Posted September 4, 2012 I don't have the book on me, but what is the purpose of the following line? task.value = ""; Also, you don't need (and shouldn't use) a for loop for the join method. Basically, what's going on in your code? Link to comment Share on other sites More sharing options...
markifornia Posted September 4, 2012 Share Posted September 4, 2012 Hartley, Since you may not have the book handy, here's the entire script for your reference: var tasks = []; function addTask() { 'use strict'; var task = document.getElementById('task'); var output = document.getElementById('output'); var message = ''; if (task.value) { tasks.push(task.value); message = '<h2>To-Do</h2><ol><li>'; for (var i = 0, count = tasks.length; i < count; i++) { message += tasks.join('<li><li>'); task.value = ""; } /* This script was used to create random tasks, but I'm commenting this out for now var randomNumber = tasks.length; var randomTask = parseInt(Math.random() * randomNumber, 10) + ''; message += '<li>Random Task:' + tasks[randomTask] + '</li>'; */ message += '</li></ol>'; output.innerHTML = message; }// end task value conditional return false; } // End addTask Function function init() { 'use strict'; document.getElementById('theForm').onsubmit = addTask; } window.onload = init; task.value = ""; The above code clears any data from the from the text input after a task has been added to the array as opposed to the same data remaining there (if you keep clicking submit, the same data is being output). It was one of the PURSUE's at the end of the chapter. It made logical sense in the script and worked fine. Unless I may be using the incorrect method. Also, you don't need (and shouldn't use) a for loop for the join method. Basically, what's going on in your code? You left a good note regarding the join method, it probably explains why I couldn't get it to work properly while using the loop method. It simply didn't make sense. There is a PURSUE at the end of the chapter asking to use the join() method in that manner. Update tasks.js so that it uses join() to create the final message, instead of concatenating together multiple strings. The only place where concatenation really occurs is in the loop, so I was attempted to make the join() method work. In this case, it may not make sense but I tried anyway. Link to comment Share on other sites More sharing options...
HartleySan Posted September 4, 2012 Share Posted September 4, 2012 Oh, okay. I misunderstood. I thought the code you were producing was from the book, and I was kinda shocked that Larry would put something like that into his book (not to rag on your code too much). Anyway, if you take the for loop outta your code, it'll all fall into place. You certainly don't want the task.value = ""; line in the for loop either. Also, careful about the string argument you're providing to the join method; you don't want two opening li tags in a row. Link to comment Share on other sites More sharing options...
markifornia Posted September 7, 2012 Share Posted September 7, 2012 ya no offense taken. the task.value=""; works outside the loop, thanks for the warning. As for the join() method, it simply doesn't make sense to use this approach inside the loops as you noted before. So I'll leave it as is. Thanks! Link to comment Share on other sites More sharing options...
Recommended Posts