checking results - a silly mistake?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Geoff Cox

    checking results - a silly mistake?

    Hello,

    I cannot see what is wrong with this.

    Even though the loop gives a value for each of the 36 values of the
    lab_result array I get the "Please answer all the questions" alert.

    Why?!

    Cheers

    Geoff

    var lab_result = new Array();
    ..
    ..
    ..
    ..

    function readyToSend() {

    for (var i=1;i<37;i++) {
    alert('lab_resu lt[' + i + '] = ' + lab_result[i]);
    }

    if (lab_result[1] && lab_result[2] && lab_result[3] && lab_result[4]
    && lab_result[5] && lab_result[6] && lab_result[7] && lab_result[8] &&
    lab_result[9] && lab_result[10] && lab_result[11] && lab_result[12] &&
    lab_result[13] && lab_result[14] && lab_result[15] && lab_result[16]
    && lab_result[17] && lab_result[18] && lab_result[19] &&
    lab_result[20] && lab_result[21] && lab_result[22] && lab_result[23]
    && lab_result[24] && lab_result[25] && lab_result[26] &&
    lab_result[27] && lab_result[28] && lab_result[29] && lab_result[30]
    && lab_result[31] && lab_result[32] && lab_result[33] &&
    lab_result[34] && lab_result[35] && lab_result[36]) {
    sendResults();
    } else {
    alert("Please answer all the questions!");
    }

    }
  • Evertjan.

    #2
    Re: checking results - a silly mistake?

    Geoff Cox wrote on 27 mei 2008 in comp.lang.javas cript:
    Hello,
    >
    I cannot see what is wrong with this.
    >
    Even though the loop gives a value for each of the 36 values of the
    lab_result array I get the "Please answer all the questions" alert.
    >
    Why?!
    >
    Cheers
    >
    Geoff
    >
    var lab_result = new Array();
    .
    .
    .
    .
    >
    function readyToSend() {
    >
    for (var i=1;i<37;i++) {
    alert('lab_resu lt[' + i + '] = ' + lab_result[i]);
    >}
    >
    if (lab_result[1] && lab_result[2] && lab_result[3] && lab_result[4]
    && lab_result[5] && lab_result[6] && lab_result[7] && lab_result[8] &&
    lab_result[9] && lab_result[10] && lab_result[11] && lab_result[12] &&
    lab_result[13] && lab_result[14] && lab_result[15] && lab_result[16]
    && lab_result[17] && lab_result[18] && lab_result[19] &&
    lab_result[20] && lab_result[21] && lab_result[22] && lab_result[23]
    && lab_result[24] && lab_result[25] && lab_result[26] &&
    lab_result[27] && lab_result[28] && lab_result[29] && lab_result[30]
    && lab_result[31] && lab_result[32] && lab_result[33] &&
    lab_result[34] && lab_result[35] && lab_result[36]) {
    sendResults();
    >} else {
    alert("Please answer all the questions!");
    >}
    >
    >}

    I cannot even read the above,
    so I too cannot see what is wrong with it,
    try:

    function readyToSend() {

    var ready = true;
    var i = 1;

    while (ready && i<37) {
    ready = !!lab_result[i++];
    };

    if (ready) {
    sendResults();
    } else {
    alert("Please answer all the questions!");
    };

    };






    --
    Evertjan.
    The Netherlands.
    (Please change the x'es to dots in my emailaddress)

    Comment

    • Geoffrey Summerhayes

      #3
      Re: checking results - a silly mistake?

      On May 27, 2:52 pm, Geoff Cox <g...@freeuk.no tcomwrote:
      Hello,
      >
      I cannot see what is wrong with this.
      >
      Even though the loop gives a value for each of the 36 values of the
      lab_result array I get the "Please answer all the questions" alert.
      >
      Why?!
      >
      var lab_result = new Array();
      ...
      >
      function readyToSend() {
      >
      for (var i=1;i<37;i++) {
      alert('lab_resu lt[' + i + '] = ' + lab_result[i]);
      >
      }
      >
      Just because something has a value doesn't mean
      it will evaluate to true.

      Try checking the 'truth' values...

      for (var i=1;i<37;i++) {
      var val=false;
      if(lab_result[i])val=true;
      alert('lab_resu lt[' + i + '] = ' + lab_result[i]+', truth
      value:'+val);

      }

      ----
      Geoff

      Comment

      • Geoff Cox

        #4
        Re: checking results - a silly mistake?

        On Tue, 27 May 2008 12:21:13 -0700 (PDT), Geoffrey Summerhayes
        <sumrnot@gmail. comwrote:
        >Just because something has a value doesn't mean
        >it will evaluate to true.
        >
        >Try checking the 'truth' values...
        >
        for (var i=1;i<37;i++) {
        var val=false;
        if(lab_result[i])val=true;
        alert('lab_resu lt[' + i + '] = ' + lab_result[i]+', truth
        >value:'+val) ;
        Geoff

        Thanks for the above - I am getting false values when the
        lab_result[i] equals zero.

        I changed from having say

        lab_result[8] = 0;

        to

        lab_result[8] = "0";

        and the code works.

        What is the subtle difference?

        Cheers

        Geoff

        >
        }
        >
        >----
        >Geoff

        Comment

        • Geoff Cox

          #5
          Re: checking results - a silly mistake?

          On 27 May 2008 19:13:11 GMT, "Evertjan."
          <exjxw.hannivoo rt@interxnl.net wrote:
          >Geoff Cox wrote on 27 mei 2008 in comp.lang.javas cript:
          >
          >Hello,
          >>
          >I cannot see what is wrong with this.
          >>
          >Even though the loop gives a value for each of the 36 values of the
          >lab_result array I get the "Please answer all the questions" alert.
          >>
          >Why?!
          >>
          >Cheers
          >>
          >Geoff
          >>
          >var lab_result = new Array();
          >.
          >.
          >.
          >.
          >>
          >function readyToSend() {
          >>
          >for (var i=1;i<37;i++) {
          >alert('lab_res ult[' + i + '] = ' + lab_result[i]);
          >>}
          >>
          >if (lab_result[1] && lab_result[2] && lab_result[3] && lab_result[4]
          >&& lab_result[5] && lab_result[6] && lab_result[7] && lab_result[8] &&
          >lab_result[9] && lab_result[10] && lab_result[11] && lab_result[12] &&
          >lab_result[13] && lab_result[14] && lab_result[15] && lab_result[16]
          >&& lab_result[17] && lab_result[18] && lab_result[19] &&
          >lab_result[20] && lab_result[21] && lab_result[22] && lab_result[23]
          >&& lab_result[24] && lab_result[25] && lab_result[26] &&
          >lab_result[27] && lab_result[28] && lab_result[29] && lab_result[30]
          >&& lab_result[31] && lab_result[32] && lab_result[33] &&
          >lab_result[34] && lab_result[35] && lab_result[36]) {
          >sendResults( );
          >>} else {
          >alert("Pleas e answer all the questions!");
          >>}
          >>
          >>}
          >
          >
          >I cannot even read the above,
          >so I too cannot see what is wrong with it,
          >try:
          >
          >function readyToSend() {
          >
          var ready = true;
          var i = 1;
          >
          while (ready && i<37) {
          ready = !!lab_result[i++];
          };
          >
          if (ready) {
          sendResults();
          } else {
          alert("Please answer all the questions!");
          };
          >
          >};
          thanks for the above - I have used similar code from Geoff and you
          will see I have seen the light!

          Thanks

          Geoff

          Comment

          • Lasse Reichstein Nielsen

            #6
            Re: checking results - a silly mistake?

            Geoff Cox <gcox@freeuk.no tcomwrites:
            I changed from having say
            >
            lab_result[8] = 0;
            >
            to
            >
            lab_result[8] = "0";
            >
            and the code works.
            >
            What is the subtle difference?
            The difference is that the number 0, when used in a boolean context,
            is converted to false, whereas the non-empty string "0" is converted
            to true.

            The non-boolean values that are converted to false are:
            0, NaN, "" (empty string), null and undefined
            All other values convert to true as booleans.

            --
            Lasse Reichstein Nielsen - lrn@hotpop.com
            DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleD OM.html>
            'Faith without judgement merely degrades the spirit divine.'

            Comment

            • Dr J R Stockton

              #7
              Re: checking results - a silly mistake?

              In comp.lang.javas cript message <uqlo349nmf9b30 f9u3ihnfckim4ta 7ddpm@4ax.
              com>, Tue, 27 May 2008 19:52:23, Geoff Cox <gcox@freeuk.no tcomposted:
              >alert("Pleas e answer all the questions!");
              But what is a valid answer?

              ISTM likely that a valid answer will need to contain at least one
              alphanumeric character, and that you might need to allow something to
              indicate that the question has not been forgotten but no real answer is
              possible; underline would serve for that.

              Do use something like
              for each input box in turn do
              OK = /\w/.test(box.value )
              if (not OK) { alert(Box N is bad) ; return false }
              end loop
              return true

              If the context allows any visible answer, but not visibly-blank one,
              change \w to \S.

              --
              (c) John Stockton, nr London, UK. ?@merlyn.demon. co.uk Turnpike v6.05 MIME.
              Web <URL:http://www.merlyn.demo n.co.uk/- FAQish topics, acronyms, & links.
              Proper <= 4-line sig. separator as above, a line exactly "-- " (SonOfRFC1036)
              Do not Mail News to me. Before a reply, quote with ">" or "" (SonOfRFC1036)

              Comment

              • Geoff Cox

                #8
                Re: checking results - a silly mistake?

                On Tue, 27 May 2008 22:50:28 +0200, Lasse Reichstein Nielsen
                <lrn@hotpop.com wrote:
                >Geoff Cox <gcox@freeuk.no tcomwrites:
                >
                >I changed from having say
                >>
                >lab_result[8] = 0;
                >>
                >to
                >>
                >lab_result[8] = "0";
                >>
                >and the code works.
                >>
                >What is the subtle difference?
                >
                >The difference is that the number 0, when used in a boolean context,
                >is converted to false, whereas the non-empty string "0" is converted
                >to true.
                >
                >The non-boolean values that are converted to false are:
                0, NaN, "" (empty string), null and undefined
                >All other values convert to true as booleans.
                Thanks Lasse,

                Cheers

                Geoff

                Comment

                • Geoff Cox

                  #9
                  Re: checking results - a silly mistake?

                  On Wed, 28 May 2008 13:42:55 +0100, Dr J R Stockton
                  <jrs@merlyn.dem on.co.ukwrote:
                  >In comp.lang.javas cript message <uqlo349nmf9b30 f9u3ihnfckim4ta 7ddpm@4ax.
                  >com>, Tue, 27 May 2008 19:52:23, Geoff Cox <gcox@freeuk.no tcomposted:
                  >
                  >>alert("Plea se answer all the questions!");
                  >
                  >But what is a valid answer?
                  >
                  >ISTM likely that a valid answer will need to contain at least one
                  >alphanumeric character, and that you might need to allow something to
                  >indicate that the question has not been forgotten but no real answer is
                  >possible; underline would serve for that.
                  >
                  >Do use something like
                  for each input box in turn do
                  OK = /\w/.test(box.value )
                  if (not OK) { alert(Box N is bad) ; return false }
                  end loop
                  return true
                  >
                  >If the context allows any visible answer, but not visibly-blank one,
                  >change \w to \S.
                  Thanks John, will give this a go.

                  Cheers

                  Geoff

                  Comment

                  Working...