SitePoint Sponsor

User Tag List

Results 1 to 3 of 3
  1. #1
    SitePoint Member
    Join Date
    Jun 2008
    Posts
    8
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    school javascript problem help!

    Hi! i've got to this problem to do and just can't do it! Anybody could help me with this one? Thanks!

    First, use a while loop and an if statement to count from 1 to 10 and add the result of each iteration to a text field. When a button is selected the text field should display 1,2,3,4,5,6,7,8,9,10

    Second, use a for loop and an if statement to count from 1 to 10 and add the result of each iteration to a text field. When a button is selected the text field should display 1,2,3,4,5,6,7,8,9,10

  2. #2
    SitePoint Guru Ize's Avatar
    Join Date
    Nov 2005
    Location
    The Netherlands
    Posts
    809
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Code:
    <button id="counter"><input id="result" type="text">
    <script type="text/javascript">
    window.onload= function (){ 
     document.getElementById ('counter').onclick = function () {
       var i=0, result = document.getElementById ('result');
       while (++i < 11) {
         result.value += i;
         if (i < 10) {
           result.value += ',';
         }
      }
    }
    Code:
    <button id="counter"><input id="result" type="text">
    <script type="text/javascript">
    window.onload= function (){ 
     document.getElementById ('counter').onclick = function () {
       var result = document.getElementById ('result');
       for (var i=1; i < 11; i++) {
         result.value += i;
         if (i < 10) {
           result.value += ',';
         }
      }
    }

  3. #3
    Unobtrusively zen silver trophybronze trophy
    paul_wilkins's Avatar
    Join Date
    Jan 2007
    Location
    Christchurch, New Zealand
    Posts
    14,526
    Mentioned
    83 Post(s)
    Tagged
    3 Thread(s)
    You could instead though just assign a function to the elements and return the value directly.

    Code javascript:
    document.getElementById('counter').onclick = function () {
        document.getElementById('result').value = function () {
            var numbers = [],
                i = 0;
            while (numbers.length < 10) {
                numbers.push(++i);
            }
            return numbers;
        }();
    }

    And for the for loop, this one is so simple that you don't even need any contents for the loop.

    Code javascript:
    document.getElementById('counter').onclick = function () {
        document.getElementById('result').value = function () {
            for (var i = 0, numbers = []; i < 10; i++, numbers.push(i));
            return numbers;
        }();
    }
    Programming Group Advisor
    Reference: JavaScript, Quirksmode Validate: HTML Validation, JSLint
    Car is to Carpet as Java is to JavaScript


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •