JS: Random Numbers and comments:, Summaries of Technology

Quick explanation: Math.random() generates a random number between 0 and 1 (e.g., 0.4). We multiply this by a range to generate a random number between 0 ...

Typology: Summaries

2022/2023

Uploaded on 03/01/2023

strawberry3
strawberry3 🇺🇸

4.6

(39)

387 documents

1 / 8

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
1
JS: Random Numbers and comments:
Random Pictures:
So far, every time you click on a picture, you get the appropriate text, but the same two pictures pop up. We may want
random pictures to pop up. We can do this using arrays and random numbers.
First, create an array of images (like you did for the previous tutorial). It can be as long as you want (I made mine hold 6
images, or imgArray.length is 6) :
imgArray = new Array()
imgArray [0] = "kittysleeping.jpg"
imgArray [1] = "kittymetric.jpg"
imgArray [2] = "kittyfur-back.jpg"
imgArray [3] = "kittysit.jpg"
imgArray [4] = "kittysmiling.jpg"
imgArray [5] = "kittypanda.jpg"
Now to display any random image in the array, you can generate a random number between 0 and the length of the array,
then access that image in the array.
JavaScript allows us to generate random numbers within a range using:
randnum = Math.floor(Math.random() * range )
Quick explanation:
Math.random() generates a random number between 0 and 1 (e.g., 0.4).
We multiply this by a range to generate a random number between 0 and the range, so, for instance, if you wanted to
generate a random number between 0 and 9, you’d do the following:
Math.random() * 9
and the result would be 3.6
To get a whole number, and not a fraction, I’d truncate (chop) off the .6 with:
Math.floor(Math.random() * 9)
which is the same as saying:
Math.floor(3.6)
which gives us 3.
Finally, I need a box to put all this in, and I called my box randnum.
So I’m saying randnum = 3,
or
randnum = Math.floor(3.6),
or
randnum = Math.floor(Math.random() * 9)
where the value 9 is the upper value of the range of numbers in which I want to generate a random number (i.e., I want to
generate a random number between 0 and 9)
pf3
pf4
pf5
pf8

Partial preview of the text

Download JS: Random Numbers and comments: and more Summaries Technology in PDF only on Docsity!

JS: Random Numbers and comments:

Random Pictures:

So far, every time you click on a picture, you get the appropriate text, but the same two pictures pop up. We may want

random pictures to pop up. We can do this using arrays and random numbers.

First, create an array of images (like you did for the previous tutorial). It can be as long as you want (I made mine hold 6

images, or imgArray.length is 6) :

imgArray = new Array()

imgArray [0] = "kittysleeping.jpg"

imgArray [1] = "kittymetric.jpg"

imgArray [2] = "kittyfur-back.jpg"

imgArray [3] = "kittysit.jpg"

imgArray [4] = "kittysmiling.jpg"

imgArray [5] = "kittypanda.jpg"

Now to display any random image in the array, you can generate a random number between 0 and the length of the array,

then access that image in the array.

JavaScript allows us to generate random numbers within a range using:

randnum = Math.floor(Math.random() * range )

Quick explanation:

Math.random() generates a random number between 0 and 1 (e.g., 0.4).

We multiply this by a range to generate a random number between 0 and the range, so, for instance, if you wanted to

generate a random number between 0 and 9, you’d do the following:

Math.random() * 9

and the result would be 3.

To get a whole number, and not a fraction, I’d truncate (chop) off the .6 with:

Math.floor(Math.random() * 9)

which is the same as saying:

Math.floor(3.6)

which gives us 3.

Finally, I need a box to put all this in, and I called my box randnum.

So I’m saying randnum = 3,

or

randnum = Math.floor(3.6),

or

randnum = Math.floor(Math.random() * 9)

where the value 9 is the upper value of the range of numbers in which I want to generate a random number (i.e., I want to

generate a random number between 0 and 9)

So, if I want to generate a random number between 0 and the length of the list, I’d use:

randnum = Math.floor(Math.random() * imgArray.length)

Now the randnum variable holds a random value between 0 and the length of imgArray.

So, putting it together, if you want a function that changes a text element, and changes two pictures to random pictures in an

array, you’d have:

Our first javascript!

New Text Goes Here

Exercise 2: Modify your web page so that it includes a counter for each image on the page and displays a count of how many

times the user clicked on each image.

Modify that so that it displays both counts each time so that the user knows how many times s/he has picked the first picture,

and how many times s/he has picked the second image.

Comments:

Every programming language allows people to include comments to themselves and to other people using

comments. Comments start with /* and end with */. Everything between these opening and closing markers is

ignored by the browser, so anything between them won’t run in javascript.

Here’s an example of a comment:

function TextRandPic(par1, par2) { /* This function takes two parameters. The first must hold the id of something with text (e.g., a paragraph, a header, etc., and the second parameter holds the id of an image on your web page. The function checks to see which image you clicked on based on the id in the second parameter, and changes the innerHTML of the text element (using the id in the first parameter). It then displays two new random pictures. */ if (par2 == "pic1") { counter1 = counter1 + 1 document.getElementById(par1).innerHTML = "You picked the first picture!" } else if (par2 == "pic2") { counter2 = counter2 + 1 document.getElementById(par1).innerHTML = "You picked the second picture!" } randnum = Math.floor(Math.random() * imgArray.length) document.images["pic1"].src = imgArray[randnum] randnum = Math.floor(Math.random() * imgArray.length) document.images["pic2"].src = imgArray[randnum] }

The comment is completely ignored by the browser. It is, in essence, a not to myself (or possibly the TA or anyone else who

might be reading your code) telling them what you’re trying to do.

Because programming languages can’t be read quite the same way that English (or other spoken languages) can be read, it is

often critical that we leave notes for ourselves and others about what code does.

Comments have another amazingly useful function. They can be used for debugging. A “bug” is a flaw in your code that

makes your code either not work at all, or work improperly. A bug can be the result of a typo, or it could be a syntax error

e.g.,

 document.images['pic1'] = "cat.jpg";

 forgetting an opening or closing { }

 if (par1 = 'pic1')

It can also be a logic error – these are the hardest errors to find.

Finding bugs is known as “debugging”. You can put comments around portions of your code to see whether that helps or

hurts your code. If the uncommented portion of your code works, that means that the problem is in the commented out

portion.

Your Turn:

Write appropriate comments, including what the parameters hold and what the function does, similar to how I added

comments to the function above, for the functions in the following code:

Add comments (1):

...

Add comments (2):

Add comments (3):

Here’s an idea!

Here’s another idea!

Here’s another idea!

Here’s another idea!

Click here for more healthy living instructions!

NOTE: If you cannot figure out what each of the above functions does without running the code, you should go back and

reread ALL the javascript tutorials until you understand them. Then fill in the comments for what each of the above

functions does. Understanding these functions is a good review for the Celebration of Technology.

To turn in:

1. Exercises 1 and 2

2. Comments for functions 1-

Extra Info:

You can change the url you are linking to using the getElementById and then setting href.

So, for instance you could write javascript code that would look something like this:

Our first javascript!

** Your Choice!

** This is the first paragraph on the page

** This is another paragraph. ** link

Click here for new link