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.

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

##### Share on other sites

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.

##### Share on other sites

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.

##### Share on other sites

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!

##### 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 randomTask = parseInt(Math.random() * randomNumber, 10) + '';

```

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 += '</li></ol>';

```

The code above keeps creating an empty list elements, each time an element is added / loop is iterated.

##### Share on other sites

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?

##### Share on other sites

Hartley,

Since you may not have the book handy, here's the entire script for your reference:

```

'use strict';

var output = document.getElementById('output');

var message = '';

message = '<h2>To-Do</h2><ol><li>';

for (var i = 0, count = tasks.length; i < count; i++) {

}

/* This script was used to create random tasks, but I'm commenting this out for now

var randomTask = parseInt(Math.random() * randomNumber, 10) + '';

*/

message += '</li></ol>';

output.innerHTML = message;

return false;

function init() {
'use strict';

}

```

```

```

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.

##### 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.

##### Share on other sites

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!

## Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

×   Pasted as rich text.   Paste as plain text instead

Only 75 emoji are allowed.

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.