Solved

JavaScript Basic Evaluation

Posted on 2013-05-14
7
193 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
Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

 
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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Article by: DanRollins
This article describes a JavaScript program that creates a maze made of hexagonal cells.  In Part 2 (http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/A_7850-Hex-Maze-Part-2.html), we'll extend the program by adding a depth-…
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
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…

757 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

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now