• Industrial Automation Engineer (unregistered)

    !!!! h eeeeee llllll ppppp !!!!!!

  • (nodebb)

    this code "must be improved", presumably by "advanced programming resources".

    What would be scarier is if the comment meant that this code must be improved because it is an advanced programming resource.

  • LCrawford (unregistered)

    Tam and Dif: can you lend a hand here? Tam? Dif? HELP???

  • Debra (unregistered)

    Yeah, Tam and Dif are the real WTF here.

  • Smithers (unregistered)

    no value at all if it's a "valid" number?

    "no value at all" is called undefined and considering they probably either just use the result as a boolean or compare it to another integer, it's functionally the same as returning 0. (I'm expecting if (validateNumber(x)) { error } for mandatory fields or if (validateNumber(x) == 2) { error } for optional ones. Maybe even a switch to give a different message on missing/invalid?) An explicit return 0 would be nicer, but it's likely not a bug.

    TRWTF is more likely that there's no var i here. While they do declare digit as a local var (and whatever tam and dif might be), they use the global variable i (specifically, window.i). So if this is called from within another loop that makes the same mistake... that loop is going to have a sudden jump.

  • Robin (unregistered)

    Bonus wtf points here for how the nested ifs are done without any curly brace blocks inside. I confess I dislike the if (something) doThisOneStatenent; idiom that's common enough, but this takes it to a new level. Sure, it works, but I'd lay money on the orig developer not actually understanding why. (It's because ifs don't need the corresponding code in braces if it's a single statement, and any if counts as a single statement.)

  • (nodebb)

    This function treats ".-.-.-.-123-.-.-.-" as a valid number, which is fantastic.

  • owlstead (unregistered)

    I'm not that much of a fan of regular expressions. Better said, I'm not a fan of big, complex regular expressions where most of the checks can be done more easily using methods that improve readability. But in this case: hey, regular expressions! Then return the value using +string?

    Don't forget to skip the initial and final spaces. We like to copy values, and sometimes miss the frickin' space at the start.

  • Darren (unregistered)

    Won't the substring call return two values - the one at position i and the one at position i+1 - rather than the one that this function expects? And as it's a zero-based array, won't the final call in the loop refer to a location (i+1) that's off the end of the array?

  • (nodebb) in reply to Darren

    From: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/substring

    The substring() method returns the part of the string from the start index up to and excluding the end index, or to the end of the string if no end index is supplied.

    Note the word "excluding".

    So, no, you're worrying about the wrong thing.

  • Tinkle (unregistered)

    Hmm, so no comma and no space? That is bad.

    However I learnt something today.

    12€50 is a valid way of writing the euro currency - at least in France.

    Numbers are hard.

  • Cole (unregistered)

    Then, of course, there's the cryptic return codes: 1 if the string is empty, 2 if it contains invalid characters, and… no value at all if it's a "valid" number?

    No, it returns something: undefined. And before someone says, "JS Bad(tm)", we're on a website about bad code :) TypeScript would've caught that, however.

  • David Hemming (unregistered)

    This wouldn't keep turning up if everyone got taught in lesson 1 of computer science that the correct way to determine if something is a number is to try and put it into a number type and catch any exceptions.

  • Jason Stringify (unregistered)

    Denilson's was doing some appliance shopping

    Denilson's what? The code is not the only thing that needs improvement.

  • (nodebb)

    Shouldn't the test for an empty string be placed BEFORE the loop, instead of inside it?

    Addendum 2023-01-19 10:10: Oops, a re-reading of the original source code shows that the empty string test is AFTER the for loop, which is still the wrong place for that check.

  • Alex Vincent (google)

    isNaN(), anyone?!?

  • Gearhead (unregistered)

    The indentation creeps me out. As does the comparison between string and integer. And the chain of ifs. And the hard-coded locale. WTamF indeed.

  • Kleyguerth (github) in reply to David Hemming

    To be fair, that strategy doesn't work in javascript. Validating a number is not exactly trivial in javascript as it is quite lenient in what it accepts. parseInt which would be the closest to "put it into a number" will accept strings like "13dsfdwe" and give you back 13. isNaN will accept numbers in scientific notation (which may or may not be desired), and if you start caring about different locales it all gets way more complicated as you cannot assume that dot is the decimal separator.

  • tbo (unregistered) in reply to Mr. TA


  • dwaz (unregistered) in reply to Mr. TA

    Even better, it treats "...--" as a number. Which, of course, is correct, as it's Morse code for 3.

  • (nodebb) in reply to dwaz

    It also treats "..." as a number, which is Morse for S ... every Morse letter code is treated as a number. Not good.

  • (nodebb) in reply to Ross_Presser

    Well every letter is also a number (you just have to use ASCII :-) ) So of cause Morse code characters should also be a number, right?

  • Meir (unregistered) in reply to Yazeran1

    “Have to use ASCII”? What, no love for EBCDIC?

    (Rhetorical question, I know.)

  • José (unregistered)

    The reporter's name "Denilson" is a hint to me that he is Brazilian, as well as the site he was browsing. Thus, for those who asked about "tam" and "dif", those are common Portuguese (BR) abbreviations of "tamanho", which translates to "size", and "diferença", or "difference". Of course, neither is used in this version of the code.


  • Pass Call (unregistered)

    The var declaration "block". The missing return for the case that doesn't exit early. Whoever wrote this has only seen Pascal before they wrote this JS.

    I'm afraid the weird nesting of ifs might be the result of missing the and operator, not knowing of && and cursing this incomplete programming language...

  • Jan (unregistered) in reply to Alex Vincent
    Comment held for moderation.
  • (nodebb)

    As for me running your own website is the key for developing your business

    Addendum 2023-01-27 10:08: And for this reason i want to create my own multivendor marketplace, because as far as i know the best online shops are Amazon and Ebay and they are exactly of this type. So i need to find a professional to create multi vendor website as there clear chances to increase my profit but also to attract attention of various clients around the world

  • giammin (unregistered)
    Comment held for moderation.
  • Mathilde (github)
    Comment held for moderation.
  • Mathilde (github)
    Comment held for moderation.
  • cassiebender (unregistered) in reply to Eroh Eroh
    Comment held for moderation.
  • sarah (unregistered)
    Comment held for moderation.

Leave a comment on “Advanced Programming Resources”

Log In or post as a guest

Replying to comment #:

« Return to Article