?
Solved

JavaScript Basic Evaluation

Posted on 2013-05-14
7
Medium Priority
?
202 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 2000 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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
LVL 6

Assisted Solution

by:nickinthooz
nickinthooz earned 2000 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 2000 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 2000 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

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

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

This article discusses the difference between strict equality operator and equality operator in JavaScript. The Need: Because JavaScript performs an implicit type conversion when performing comparisons, we have to take this into account when wri…
This article demonstrates how to create a simple responsive confirmation dialog with Ok and Cancel buttons using HTML, CSS, jQuery and Promises
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…

718 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