Help with exercise HTML/JAVASCRIPT

homework

#1

I have a homework where Im supposed to create a javascript function that shows the multiples of a number when setting limits for example:

showMultiples(13, 0, 500); and it will show me the multiples of 13 between 0 and 500. Im having problems creating the loop inside the function. any tips?


#2

What do you have so far? May we see your code, please?


#3

One sec, give me 2 mins to translate to english and I'll post it


#4
<head>
	<meta charset="utf-8" />
	<title>Exercise.04.01</title>
	<style>
	</style>
	<script>
	var value, limMin, limMax;
	function showMultiples(value, limMin, limMax){
	document.write('<h3>Multiples of ' +value+ ' between ' +limMin+ ' and ' +limMax+ '</h3>');
	var i;
	for (i == value; n <= limMax && n>= limMin; n + value ) {
	document.write(n +'; ');
	}
	}
	</script>
</head>
<body>
	<script>
	showMultiples(13, 0, 500);
	showMultiples(25, 1000, 2000);
	showMultiples(111, 1900, 4000);
	</script>
</body>

Im aware the loop is weird, Its like the 8th try and I dont seem to have any idea how to do it


#5

In the above, n is undefined. The script should be raising an exception (check your JavaScript console for the error message).

You're using the value as a starting point for the loop, with i being the iterator. What happens when the value is not in the prescribed range, limMin..limMax?

Give this some thought repost when you have another example.

Furthermore, what happens with the starting value (limMin) is not itself evenly divisible by value? Your approach appears to be addressing this. Waiting to see what you come up with.


#6

Sample output:

Eg. 1

showMultiples(13, 0, 500)
[0, 13, 26, 39, 52, 65, 78, 91, 104, 117, 130, 143, 156, 169, 182, 195, 208, 221, 234, 247, 260, 273, 286, 299, 312, 325, 338, 351, 364, 377, 390, 403, 416, 429, 442, 455, 468, 481, 494]

Eg. 2

showMultiples(13, 100, 500)
[100, 113, 126, 139, 152, 165, 178, 191, 204, 217, 230, 243, 256, 269, 282, 295, 308, 321, 334, 347, 360, 373, 386, 399, 412, 425, 438, 451, 464, 477, 490]

This illustrates two things. 1. 0 is undefined, so not a multiple of anything; and, 2. 100 is not evenly divisible by 13, so the entire list is wrong.


#7
var value, limMin, limMax;
function showMultiples(value, limMin, limMax){
document.write('<h3>Multiples of ' +value+ ' between ' +limMin+ ' and ' +limMax+ '</h3>');
var n;
for (n == value; n <= limMax && n>= limMin; n + value ) {
document.write(n +'; ');
}
}
</script>



While translating I forgot some 'I's, my bad.
Anyway I dont have a console log ( I think) to see the errors as Im using notepad++

The thing is I cant get any output, shouldnt it display anything?
This is what it should display if it was well written( https://s21.postimg.org/axu7mgerb/Capture.png )
PS: multiplos = multiples. And in case you're wondering, yes, Im lost...


#8

The JavaScript Console is available in the Tools or Developer menu of your browser, or right click an element and click, Inspect element. Then click the Console tab. Refresh the page to see new error messages.

Edit: I see you are using Chrome. Ctrl+Shift+J.

The console has a command line where you can enter a block of code and execute it immediately.

The above needs to be written as a statement:

n = n + value

or

n += value

so the increment takes place. At least then you will have some sample output to examine.


#9

Even replacing it with ' n += value' , no output received, so I have no idea where to look for the errors. but thats one place to start I guess


#10

Ctrl+Shift+J to see the error message.

i == value

is a comparison. We need to write it as an assignment:

i = value

#11

Thats where I usually look but its empty


#12

Keep the console open, fix your code and refresh the page after you save the code.

I did look at the screen shot and would still maintain that 0 is not a multiple of 13.


#13

idk why but still empty ahah, even so when I fixed some things I finally got some output so I have you to thank :wink:
Finally have something to work with, let me see if I can get this going!


#14

That means there were no errors thrown. Do you have a plan for how to address my point # 2, above?


#15

I get what you mean now, Im gonna look into it, I was so fixed in the solution (https://s21.postimg.org/axu7mgerb/Capture.png) that I wasnt thinking about it.

Btw in the solution the 0 appears, is it an error from my professor or... what do you think?


#16

PS: I fixed it, 0 gone but Im reading some posts where 0 is supposed to be a multiple f every number except 0


#17

Right now I've tried multiple ways where the first 2 outputs are right but the multiples of 111 are wrong.


#18

Zero is undefined so any definition we give to it, such as being a multiple of any number, is contrived. It's undefined. 0 is not a multiple of anything, but it does have a property which is applicable to all numbers... n * 0 is 0. This in no way makes n a factor of 0, which is my point, and the reason I wouldn't have it in the list.

If you need to include it in your exercise, then include it. Personally, I would ask the professor for a clarification. The reasoning could go either way, and right now I'm not standing on hard ground, but rationalized conjecture.

Now there is just point 2. The starting point.

Sample correction of above Eg. 2.

showMultiples(13, 100, 500)
[104, 117, 130, 143, 156, 169, 182, 195, 208, 221, 234, 247, 260, 273, 286, 299, 312, 325, 338, 351, 364, 377, 390, 403, 416, 429, 442, 455, 468, 481, 494]

#19

Yeah I found the problem, It is starting on the minimum limit and it adds from there which means my starting point on the 'for' loop is wrong, the other 2 are right thanks to a coincidence but the 3rd one showed me the mistake where it starts in 1900 and adds 111 to it and moves on to the max limit, basically I need to set it up so it starts on the lowest multiple after the the minimum limit right?


#20

Yes, precisely. Do you have a game plan?