Solved

JavaScript Basic Evaluation

Posted on 2013-05-14
7
198 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone 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

Suggested Solutions

Avoid defining the variables in the global scope; trying to define them in a local function scope. Because:   • Look-up is performed every time a variable is accessed.   • Variables are resolved backwards from most specific to least specific scope…
Introduction HTML checkboxes provide the perfect way for a web developer to receive client input when the client's options might be none, one or many.  But the PHP code for processing the checkboxes can be confusing at first.  What if a checkbox is…
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…

756 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