/    Sign up×
Community /Pin to ProfileBookmark

[RESOLVED] JavaScript For Loop changing hard code to using length property

Another assignment I’m not understanding. I’ll post the original code which I re-typed and everything ran fine. Now I need to make the following changes to the code:

[COLOR=”#0000CD”]Task 2: Make modifications in the sample code (see below)
Make modifications to the textbook example (Converting a Series of Fahrenheit Values) and instead of hard coding the condition state value of the loop (line 9) and initial state value of the loop (line 13), use the length property.[/COLOR]

[CODE]<!DOCTYPE html PUBLIC “-//W3C//DTD XHTML 1.0 Transitional//EN” “http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd”>
<html xmlns=”http://www.w3.org/1999/xhtml”>
<body>

<script type=”text/javascript”>

var degFahren = new Array(212, 32, -459.15);
var degCent = new Array();
var loopCounter; [COLOR=”#FF0000″]//Line 9[/COLOR]

for (loopCounter = 0; loopCounter <= 2; loopCounter++)
{
degCent[loopCounter] = 5/9 * (degFahren[loopCounter] – 32); [COLOR=”#FF0000″]//Line 13[/COLOR]
}

for (loopCounter = 2; loopCounter >= 0; loopCounter–)
{
document.write(“Value ” + loopCounter + ” was ” + degFahren[loopCounter] +
” degrees Fahrenheit”);
document.write(” which is ” + degCent[loopCounter] +
” degrees centigrade<BR>”);
}

</script>

</body>
</html>[/CODE]

Based on the instructions I take Line 9 and would change it from var loopCounter; to var len= temp.length; which is the initial array of temps 0, 1, 2? (I’m also looking at examples on W3Schools.)

For Line 13 degCent[loopCounter] = 5/9 * (degFahren[loopCounter] – 32); I would change loopCounter to temp.length;?

Will I not also have to change every other place loopCounter is used which he doesn’t state in the instructions?

Thank you!

to post a comment
JavaScript

9 Comments(s)

Copy linkTweet thisAlerts:
@rootOct 13.2014 — [CODE]var loopCounter; //Line 9[/CODE]

Is just not needed, all you need do is

[CODE]for (var loopCounter=0; loopCounter<=2; loopCounter++)
{[/CODE]


also...

document.write("

is a big no no in web coding, you should be updating the DOM tree with a new element.

Look at how to append an element like a DIV tag to see what I am referring to.

Then you can insert your output data in to those DIV's you add.
Copy linkTweet thisAlerts:
@iBeZiOct 13.2014 — The assignment is just asking you to use the "length" property that all arrays have within the for loops, basically you just need to replace the number 2 with "degFahren.length - 1" in the for loop conditions so it goes through all the numbers in the array.
Copy linkTweet thisAlerts:
@BlondieCauthorOct 13.2014 — Hi iBeZi, I think I understand what you're saying and my code does run after your suggested changes are made. I'm still at a loss as far as understanding what the instructor wants us to do with Lines 9 and 13 though.
Copy linkTweet thisAlerts:
@iBeZiOct 13.2014 — The instructions don't match those line numbers, maybe you're not supposed to count the empty lines making line 9 and 13 fall on the 2 loops?
Copy linkTweet thisAlerts:
@BlondieCauthorOct 13.2014 — I also wondered if he was off on the line count. In the text book the DOCTYPE line takes two lines. I went to the online site and downloaded the example so I could see the line count for myself since the text book doesn't show the line count. Of course in the editor the DOCTYPE line is only one line.

It would have been far more helpful if he had typed the line content. If he's counting as per the text book - counting only lines with code even the single curly brace and skipping the blank lines then the line numbers he gave us make sense.

It's these little things that add to my confusion while trying to learn - and frustrate me. I'm used to looked at the line number as per the editor and going by that.

Implementing what you said does work although I'm backtracking in the text book to see where we covered that other than in arrays.

I'm giving the final part of this assignment a go right now and will see how I get through it.

Thank you so much for helping and explaining.
Copy linkTweet thisAlerts:
@deathshadowOct 13.2014 — Yeah, that's what I've come to expect from a textbook example -- inept re-re code filled with pointless redundancies... just more proof most "educators" aren't qualified to be teaching jack *** to anybody.

Here are some tips:

1) if you are declaring a bunch of var in a row, you only need to say VAR once then delimit them with comma's.

2) "new Array" is old school Nyetscape coding from the 1990's. Just say []! You don't need that garbage anymore in JavaScript. (the same is true in PHP 5.4/newer)

3) Don't waste time running the same loop more than once.

4) With a command like document.write, don't waste time using more than one call to the same function!

5) Counters are typically done with i, j and k in most C syntax languages, or t, n and v in some other langauges. "loopCounter" is acceptable, but a bit wordy for a temporary index.

&lt;script type="text/javascript"&gt;

var
degF = [212, 32, -459.15],
degC = [];

for (var i = 0; i &lt; degF.length; i++) {
degC[i] = 5/9 * (degF[loopCounter] - 32);
document.write(
'Value ' + i +
' was ' + degF[loopCounter] + '&amp;deg; Fahrenheit' +
' which is ' + degC[loopCounter] + ' "degrees" centigrade&lt;br /&gt;'
);
}

&lt;/script&gt;


... and I do mean those double quotes around "degrees" when talking the arbitrary and silly metric nonsense. Decimal is such a stupid numbering system. Ooh, we have four fingers and two thumbs, let's base EVERYTHING on that, who cares if the precision in common applications is rubbish... :p

[i]Sorry, sexagesimal base and polar coordinate fan checking in... The Sumerians had it right![/i]
Copy linkTweet thisAlerts:
@iBeZiOct 14.2014 — Deathshadow, this is clearly just a learning exercise, it isn't supposed to be super efficient, the variable is called loopCounter because that makes it easy to see the purpose of that variable, and there's two for loops because that's the point of this exercise, to demonstrate how for loops work.

I agree the old Array syntax should probably be scrapped, but again, it's probably there because by using "new Array" it's easier to see that you're making an array than by using [], which is probably explained in a later exercise...
Copy linkTweet thisAlerts:
@deathshadowOct 14.2014 — Deathshadow, this is clearly just a learning exercise[/quote]
Then it should be teaching people how to do things up front -- but I guess like that "core education" garbage that's no longer a priority. Instead let's just dumb everything down so nobody learns to do anything properly if at all. Then we wonder why people are graduating high schools with the equivalent of a 5th grade education from 30 years ago.

... which is of course why conventional education was useless to me; but let's face it, the modern educational system seems more about making a buck and putting people decades into debt than it is about teaching anyone anything of value.

But of course, that's why we've gone and made the average college diploma worth less than a sheet of bog roll in most every field that involves real work; no matter how many ignorant jacktards seem to think otherwise.

Sorry, is my disgust to the point of nausea with the filthy little degenerates who call themselves "educators" boiling over?
Copy linkTweet thisAlerts:
@rootOct 14.2014 — This will also do the trick.
[CODE]<script>
var degF = [212, 32, -459.15],
degC = [];

while(degF.length){
df=degF.shift()
degC.push( df,'&deg;F= ',(5/9 * (df - 32)),'&deg;C <br />rn' );
}
document.write(degC.join(""));
</script>[/CODE]
×

Success!

Help @BlondieC spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.18,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...