Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

JavaScript Basic Evaluation

Posted on 2013-05-14
7
Medium Priority
?
203 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 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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
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

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!

Question has a verified solution.

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

Having worked on larger scale sites, we found out that you are bound to look at more scalable solutions to integrating widgets, code snippets or complete applications and mesh them into functional sites, in any given composition. To share some of…
Today, the web development industry is booming, and many people consider it to be their vocation. The question you may be asking yourself is – how do I become a web developer?
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…
Suggested Courses

885 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