Jump to content
Larry Ullman's Book Forums


  • Content Count

  • Joined

  • Last visited

  • Days Won


HartleySan last won the day on September 25 2015

HartleySan had the most liked content!

Community Reputation

813 Excellent

About HartleySan

  • Rank
    Advanced Member

Recent Profile Visitors

The recent visitors block is disabled and is not being shown to other users.

  1. HartleySan

    Chapter 5 Calculator

    John, we talked about this in your last post, but you cannot do mathematical addition on strings. Whenever you do .value on a DOM form input, you are going to get a string back, even if it looks like a number. That will always be the case. And when you start trying to do fancy math with strings, you'll almost always end up with NaN (or not-a-number). NaN is not a syntax error, that's a logic error in that you're trying to do number math with strings. Please fix that problem and you should be okay.
  2. HartleySan

    Chapter 5 Calculator

    Your if condition says, "If the number of hours worked is less than 80 and less than 100, then do something." I think that's your problem. Anything under 100 hours will net you the same result every time.
  3. Speaking in high-level generalities, I think there are two parts to this: Identifying the events that will cause notifications to be created; and Deciding how to display notifications to the users. An example of #1 might be creating a notification for a topic creator every time someone posts something in one of their topics. For example, you might create a notification with the ID of the person who posted in the topic and then the ID of the topic creator, who will get the notification. You can then decide how you want to notify the topic creator. Do you want to send them an email? Do you want to do what Stack Overflow does and have an icon at the top of the site? Do you want both? If you go with the icon approach, then the simple thing to do is to do a check for unread notifications directed at a particular user every time a user loads a page on the site. You could display the number of unread notifications or something, and once they read the notifications, then the number goes down. That's the basic gist. If you want to go into more detail, then we can, but hopefully that's a good start.
  4. Here's a good starting point: https://www.w3schools.com/html/html5_geolocation.asp You have to do this in JS though, not PHP, and if the browser doesn't support navigator.geolocation, then you have to fall back on some PHP alternative, which is up to you to decide. Please note that the Geolocation API is built into regular JS, and it's separate from the Google Maps API, which is provided by Google. However, if you use the Geolocation API to get the user's current location, then you can feed the acquired coordinates into the Google Maps API from there. Make sense?
  5. HartleySan

    Chapter 4 Calculator

    John B, glad you got it working. The only thing I will say in response as a critique is that I think that things like the following are a bit difficult to understand: +regularPay+ +overtimePay People familiar with JS will know that +numInStrFormat will convert a String number to a Number number, but it does make the code a bit difficult to understand at first glance. In general, the easier code is to read, the better. All the same, glad you got it working, and it seems like you learned a lot along the way, which is great.
  6. HartleySan

    Chapter 4 Calculator

    Sounds good. I don't see a screenshot, but good luck with all of that, and let me know if you need anything else. Thanks.
  7. HartleySan

    Chapter 4 Calculator

    Hmmm... hard to say without a bit more context. I generally use Chrome for development, as I find the dev tools to be more to my liking. As such, I haven't used FF in a while to dev. With that said, I vaguely recall there being something in FF with the fact that if an error occurs before you load the console, then it wouldn't show the error in the console. Maybe try loading the console in FF first, then reloading the page and seeing if the error shows up. Beyond that, it's hard for me to assist without knowing exactly what you're doing and how. Thanks.
  8. HartleySan

    Chapter 4 Calculator

    John B, the browser console is your best friend for trying to debug JS issues/errors. I loaded up your code and instantly saw the following error: The following is line 23: var totalTaxes = document.getElementById ('totalTaxes').value = parseFloat(ficaTax+stateTax+federalTax*grossPay)/100.toFixed(2); An unexpected token error basically means that there is some invalid/unknown character in your code. This one is a bit interesting though in that the error is actually with the following expression: 100.toFixed(2) The reason this is a problem is because the 100. is ambiguous. Specifically, when you put a period right after a number like that, the JS parser doesn't know whether to treat that like a decimal in a number (e.g., 100.00) or as a dot used to invoke a method of the Number object (e.g., toFixed(2)). The easiest way to resolve this error is to remove the ambiguity by doing something like the following: (100).toFixed(2) With all that said, I still think there are some other logic errors in your code. If I were to make a recommendation, I would try to take a lot of the repetitive things you're doing and pull them out into one or more functions to decrease the likelihood of bugs in your code as well as make it simpler. As some examples, you could turn document.getElementById(str) into a function. You could also turn document.getElementById(str).value into another function that utilizes the first function. Furthermore, you could add a flag for whether to convert to a string or a number. Lastly, I would probably write a function to then set the value of a DOM input element. Putting all of that together, I may rewrite your code as follows: function calculate() { 'use strict'; var regularHours = getVal('regularHours', true), overtimeHours = getVal('overtimeHours', true), hourlyRate = getVal('hourlyRate', true), ficaTax = getVal('ficaTax', true), stateTax = getVal('stateTax', true), federalTax = getVal('federalTax', true), regularPay = to2Dec(regularHours * hourlyRate, true), overtimePay = to2Dec(overtimeHours * hourlyRate * 1.5, true), grossPay = to2Dec(regularPay + overtimePay, true), totalTaxes = to2Dec((ficaTax + stateTax + federalTax) * grossPay / 100, true), // Not sure about the correct parens here for the math. netPay = to2Dec(grossPay - totalTaxes, true); setVal('regularPay', regularPay); setVal('overtimePay', overtimePay); setVal('grossPay', grossPay); setVal('totalTaxes', totalTaxes); setVal('netPay', netPay); setVal('employeeName', getVal('firstName') + ' ' + getVal('lastName')); // Need the comma with first name first? return false; } function getVal(id, toNum) { var elem = gI(id); if (toNum) { return parseFloat(elem.value); } return elem.value; } function setVal(id, val) { gI(id).value = val; } function gI(id) { return document.getElementById(id); } function to2Dec(num, toNum) { // To two-decimal number if (toNum) { return parseFloat(num.toFixed(2)); } return num.toFixed(2); } function init() { 'use strict'; gI('form1').onsubmit = calculate; } window.onload = init; In my mind, that's a lot easier to debug as well as read. Please share your thoughts and any other questions you have. Thanks.
  9. HartleySan

    Chapter 4 Calculator

    If you do a Google search for something like "JS string to number", you'll get a multitude of good resources. Here's the first hit: https://gomakethings.com/converting-strings-to-numbers-with-vanilla-javascript/ I generally use parseInt(str, 10), and since that gets annoying to type after a while, I have that logic bundled up until a function called toInt. One nice thing about coding, and JavaScript in particular, is that you can Google just about anything. Just type "JS" and then whatever you're looking for, and you'll likely get a Stack Overflow hit or something else that's relevant. As for your second question, when you ask "will the values for regularPay and overtimePay still appear in the calculator," I assume you're asking whether a number can be stored into the value property of a DOM input element. If that's the case, then yes. JS will automatically perform string interpolation for you. That is to say, if you try to stick a number into something that requires a string, JS will automatically convert it to a string for you. For example, if I do the following: '10' + 10 I'll get the string '1010', not 20. That's because when you try to do a string "plus" a number, JS doesn't try to do the math, it converts the number to a string and then concats the two strings together. As such, yes, you can stick a number into the value property of a DOM input element, and it'll automatically turn it into a string to be displayed on the screen. Hope that all makes sense.
  10. gbengasupowale807, I took a look at your code, and like Larry suggested, it helps a lot of you use your browser console. In this case, it looks like the browser is reloading the page after the JS error occurs, thus making it hard to catch the error. However, if you check the Preserve log option in Chrome (or the similar option in the browser of your choice), you should then see the error upon form submission. Long story short, you're trying to reference the value property of the testimony DOM element on line 4 of testimony.js. However, the problem is that testimony is a form DOM element, which doesn't have a value property, thus the issue. Resolve that, and hopefully, you can progress forward with your code.
  11. HartleySan

    Chapter 4 Calculator

    John, I can see where things can get a bit confusing. When you run the toFixed method on a number, the result is a string, not a number. As an example, in your code, both the regularPay and overtimePay variables are actually strings, not numbers. As such, regularPay + overtimePay is not going to give you what you would expect. What's more, when you try to run the toFixed method on the result of regularPay + overtimePay, you will get an error because only numbers, not strings, even have the toFixed method available to them. For the sake of your calculations, you may want to convert the result of toFixed each time back into a number. Ultimately, it's up to you how you want to handle it though. As another note, it often helps to load your browser JS console up when you're writing JS code like this. It'll typically tell you when there's an error as well as what kind of error it is and where it's located. It makes these sorts of things easier to debug. Hope that all makes sense and good luck. Feel free to response with any other questions. Thanks.
  12. HartleySan

    What Language Should I Learn?

    Maybe try Codecademy.com to start with and play around with the various languages there until you find one that suits you.
  13. Try echoing out the $curtain_price_total and $cur_v values each time through the loop, and make sure they are what you expect.
  14. There are a couple of things I can suspect: You didn't initialize $curtain_price_total to 0. $cur_v is a string, not a number. By default, all values pulled from a MySQL DB are strings. Either of those relevant?
  15. HartleySan

    Confusion About Flex

    Personal opinion, but if you want an OS-agnostic application, the web is generally your best bet. It offers the most flexibility, and there's very little you can do in desktop applications these days that you can't do on the web. Just my two cents.