Jump to content
Larry Ullman's Book Forums

Ch 6 - Pursue 3 - Need Help


Recommended Posts

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

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.

  • Upvote 2
Link to comment
Share on other sites

  • 2 weeks later...

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

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

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

 Share

×
×
  • Create New...