1
  • document[]

  • src.charAt

  • src.length

What are these three things?

I am sure that pixState will give me a 1 or a 0;

var pixState = document[imgName].src.charAt((document[imgName].src.length) - 5)

Here is one of the link:

<a href="javascript: togImage(&quot;p47&quot;)"><img src="pixel0.gif" name="p47"></a>

function togImage(imgName) {
  var bitMask = 1 << parseInt(imgName.charAt(2))
  var n=imgName.charAt(1)
  var pixelChange = 'document.pixCalc.row'+n+'.value = bitMaps[n]'
  var pixelChange1 = 'document.pixCalc.row'+n+'1.value = "0x"+bitMaps[n].toString(16)'
  var pixState = document[imgName].src.charAt((document[imgName].src.length) - 5)

  if (pixState == '0') {
    document[imgName].src = 'pixel1.gif'
    bitMaps[n] = bitMaps[n] | bitMask
    eval(pixelChange)
    eval(pixelChange1)
    upDateList()
  } else { 
    document[imgName].src = 'pixel0.gif'
    bitMask = bitMask ^ 0xFF
    bitMaps[n] = bitMaps[n] & bitMask
    eval(pixelChange)
    eval(pixelChange1)
    upDateList()
  }
}
Seth McClaine
  • 9,142
  • 6
  • 38
  • 64
Atmega 328
  • 93
  • 5
  • It would be helpful to potential answerers to format your code. – CollinD Apr 18 '17 at 19:49
  • It would be helpful to you to search for the answer rather than making other people do it for you... [`charAt`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/charAt), [`length`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/length)... `document[]` merely uses [bracket notation](http://stackoverflow.com/q/20736758/215552). – Heretic Monkey Apr 18 '17 at 19:57
  • I did for at least a hour, but I couldn't figure out the right answer to that. – Atmega 328 Apr 18 '17 at 20:00
  • oh my, this code is bad. Not bad like *a newbie that doesn't know better*, but like *a total noob who has no idea how little he/she knows, but wanted to be fancy*. I'd reccomend to delete the file, format the drive it was on, then use a sledgehammer on the computer, grab a new computer and start building this from scratch. – Thomas Apr 18 '17 at 20:09
  • @Thomas Why not try to be constructive? This user identified their weak points and displays a willingness to improve. We were all new once (and many still are), no need to discourage someone trying to learn. The larger concepts are sometimes easier to understand after rolling around in some muck, so you properly understand why you're doing things in a particular fashion. My first arts-and-crafts project in grade school was just as bad (if not worse) than my first sample of code, but I've learned from both of those adventures (and set neither on fire). – Hodrobond Apr 18 '17 at 20:22
  • 1
    well, if the code works, then it's good for me. I don't really get discourage by negative comments. – Atmega 328 Apr 18 '17 at 20:26
  • @Hodrobond, I'm not trying to discourage anyone. Telling by the question, it's not his code we're talking about. I just wanted to point out that this code is really awful and mock that a bit. And I'd highly encorage Atmega to not take this code as an example how to code but instead write the logic he needs from scratch, really. And I'm perfectly fine helping if there are questions along that way, but with this code, there are so many bad decisions that I don't know where to start fixing it. – Thomas Apr 18 '17 at 20:29
  • @Thomas I don't disagree that a rework would be greatly helpful, and that discussion is significantly larger than this question. "but with this code, there are so many bad decisions that I didn't know where to start fixing it." --> answer "What are these three things?", it's a question about the basics of HTML and JavaScript. If they have another question down the road they'll come back. – Hodrobond Apr 18 '17 at 20:33
  • 1
    @Hodrobond "I don't disagree that a rework would be greatly helpful" yeah, this would definitely be off topic, but I still think It would pay off down the road. I've learned a lot, by building things myself and not just copy pasting someone elses code, and by reading other peoples code. What I saw here was someone new to programming learning from this code. I'm good with how this little discussion went, let us stop, and "If they have another question down the road they'll come back" I really hope so. – Thomas Apr 18 '17 at 21:04
  • @Atmega, I don't insist on anything, Just wanted to give you a little push to reconsider wether you should take this as an example on how to program, and raise the question **in you** why this ain't good code and how to do better. – Thomas Apr 18 '17 at 21:09

3 Answers3

2

The document refers to the Document in the DOM. It stores all of the information about the HTML page.

charAt is a String prototype function. Therefore it may be used on any string, such as "something".charAt(1). It will return the character at that index of the String.

length is a property on many elements, including Strings, Arrays, etc. They traditionally return the number of elements represented in the object (number of letters in a string, number of elements of an array, etc).

Hodrobond
  • 1,665
  • 17
  • 18
  • so document[imgName].src.charAt = "pixel0.gif".charAt; and (document[imgName].src.length) = "pixel0.gif".length; So "pixel0.gif".charAt (10-5); or "pixel0.gif".charAt (5) = 0? – Atmega 328 Apr 18 '17 at 20:05
  • correct! First the document[imgName] will return the `img` element, the `.src` will return the image source (as a string), on which you can use `charAt`! – Hodrobond Apr 18 '17 at 20:06
  • since i know the scr, then when I replace "(document[imgName].src.length) - 5" with 5, it doesn't work! – Atmega 328 Apr 18 '17 at 20:08
  • The following returns 0 or 1 for me (if the image name is pixel1.gif vs pixel0.gif) `document["testImage"].src.charAt(document["testImage"].src.length - 5)`. Please check this [jsfiddle](https://jsfiddle.net/2jjvr61q/1/). What is it you need this for? – Hodrobond Apr 18 '17 at 20:15
  • I get it now. (document[imgName].src.length) will return different number depends on where it's located. ((document[imgName].src.length) - 5) will return the sixth letter from the image file name, which is either 0 or 1 in my case. – Atmega 328 Apr 18 '17 at 20:21
  • `(document[imgName].src.length) - 5` will return "the length minus 5", so not the sixth letter from the start, but the fifth letter from the end. – Hodrobond Apr 18 '17 at 20:24
  • since. charAt starts with zero and pixel0.gif has 0 at sixth letter, I think it's sixth. – Atmega 328 Apr 18 '17 at 20:29
  • Remember, it's counting backwards, since it's `length - 5`! pixel0.gif --> fig.0lexip. 1-"f" 2-"i" 3-"g" 4-"." 5-"0" – Hodrobond Apr 18 '17 at 20:30
  • I don't think you are right. Check this: https://www.w3schools.com/jsref/tryit.asp?filename=tryjsref_charat Also, I have almost done with this cool little thing. You can check it out here: http://flyandance.atspace.cc/Others/matrix/matrix_2.0.html – Atmega 328 Apr 18 '17 at 23:22
  • @Atmega328 It's only backwards if you use `someString.charAt(someString.length - x)`, where x is the index from the end (normally `someString.charAt(x)` is the character at index x), [jsfiddle](https://jsfiddle.net/59j32khg/), and I like your matrix manipulator, good job! – Hodrobond Apr 19 '17 at 15:35
1

So basically pixState is finding the 5th from the last character, or in this case the last character before the file extension in the image src which is 0.

Seth McClaine
  • 9,142
  • 6
  • 38
  • 64
1

These three things are functions.

When you access document['bar'], you're gonna receive back a DOM element from your HTML. Just like <img> tag.

The src.charAt and src.length are functions related to the attribute src of your <img> tag.

charAt returns the character at determined position passed as parameter and the length's function returns the length of your string attribute (src).

Is this which you wanna know?

Julian
  • 33,915
  • 22
  • 119
  • 174
Pablo Castro
  • 134
  • 6