Solved

JavaScript Basic Evaluation

Posted on 2013-05-14
7
197 Views
Last Modified: 2013-05-14
I've been given a couple simple tasks for an evaluation that I'd like to verify.
1) The first is :
Please implement a JavaScript function called “thisisaloop” that initializes a variable called ‘dl’ with a value of ‘0’ and uses a for loop to increment the value of ‘dl’ by 1 on each iteration. Allow the for loop to execute until ‘dl’ possesses a value of ’10’, then have the function return the value of the ‘dl’ variable.

Here is my answer- will this work?
function thisisaloop()
{
var dl=0;
for (var i=0;i<10;i++)
{
dl=dl+1;
}
return dl
}

2) The second is to point out the errors in a JavaScript below. It is meant to run a while loop that adds 1 to the value of the 'dl count' variable upon each iteration, then returns the value of 'dl count' when the value reaches 100.

Line #
01 <script type="text/javascript">
02 var result = dlRules(0);
03
04 function dlRules (dl_count) {
05     var limit = 100;
06     match = 1;
07
08     while (dl_count != limit)
09              dl_count ++ match;
10              if (dl_count == 95) {
11                       dl_count = 10;
12              }
13     }
14 return (dl_count)
15 }
16 </script>
0
Comment
Question by:JSNovice
  • 4
  • 3
7 Comments
 

Author Comment

by:JSNovice
ID: 39165719
Adding additional points.
0
 
LVL 6

Assisted Solution

by:nickinthooz
nickinthooz earned 500 total points
ID: 39165778
01 <script type="text/javascript">
02 var result = dlRules(0);
03
04 function dlRules (dl_count) {
05     var limit = 100;
06     match = 1;
07
08     while (dl_count != limit)
09              dl_count ++ match;
10              if (dl_count == 95) {
11                       dl_count = 10;
12              }
13     }
14 return (dl_count)
15 }
16 </script> 

Open in new window


I'm by no means a javascript expert but here's what i think I see wrong...

 dl_count ++ match;

dl_count ++
that will increment by one without the match variable.

 if (dl_count == 95) {
11                       dl_count = 10;
12              }

that says if the count is 95, go back to 10, so technically it'll never reach 100
0
 

Author Comment

by:JSNovice
ID: 39165826
I definitely think that the issues you point out are part of what would be incorrect, that math won't allow the result to be shown. Being a novice myself, I'm also thinking that there are some statements and expressions that might be incorrectly identified/coded. Is the dlRules treated properly?

Also, for part 1 of the question- will the code I put together return  the correct result?
0
Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

 
LVL 6

Assisted Solution

by:nickinthooz
nickinthooz earned 500 total points
ID: 39165856
Your first one is fine, here's a fiddle with it:

http://jsfiddle.net/PH85K/
0
 
LVL 6

Assisted Solution

by:nickinthooz
nickinthooz earned 500 total points
ID: 39165873
Here is a fiddle that shows #2 looping to 100:

http://jsfiddle.net/PH85K/1/

function dlRules (dl_count) {
     var limit = 100;
     var dl_count = 0;
     

     while (dl_count != limit)
     {
              dl_count ++;
            }
 return (dl_count)
 }

Open in new window


of course the dl_count wouldn't be in there, it'll be passed from the function(dl_count)
0
 

Author Comment

by:JSNovice
ID: 39165929
So would part of the error be that the mention of 'match' in lines 6 and 9 as well as the 'if' statement in lines 10 and 11 are superfluous?

Total novice here so also curious about lines 01 and 16- are those necessary to identify the code in the page and to tell when to start and stop?
0
 
LVL 6

Accepted Solution

by:
nickinthooz earned 500 total points
ID: 39165945
Yes, the If statement as well as the "match" is not neccessary.  Also the match = 1 is not needed.

You have to surround your javascript in tags to get it to work so yes, those are important and you have to keep those.  However, the type = text/javascript is default, and not needed. (this may only be for html5, it may be required in html4)  

so
<script type="text/javascript">

Open in new window


can be

<script>

Open in new window

0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Today I would like to talk about localizing (Internationalization) JavaScript applications. Introduction When creating an application that is going to be used by many people around the globe, it is important to remember that not everyone speak…
I've been trying to accomplish this for a while and it just struck me yesterday how to accomplish this task. I have done searches all over the internet looking for ways to email pages from my applications and finally I have done it!!! Every single s…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

792 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question