0

So, I'm pretty new to JavaScript and I'm trying out some beginner projects on my own. I want to make a clock and add a 0 before single digit times. I have achieved this through multiple duplicate if-statements but when I try to translate that to a function I fail. I assume it is not working because I am trying to set a value to another value or I am comparing them by accident. I am in a bit of a mental knot at the moment.

function setTime() {
    var today = new Date();
    let hour = today.getHours();
    let minute = today.getMinutes();
    let seconds = today.getSeconds();
    let period = '';

    if (hour <= 11) {
        period = 'AM';
    } else if (hour >= 12) {
        period = 'PM';
        hour -= 12;
    }

    // function twoDigit(timeValue) {
    //     if (timeValue.toString().length === 1){
    //         timeValue = '0' + timeValue;
    //     }
    // }

    // twoDigit(seconds);
    // twoDigit(minute);
    // twoDigit(hour);

    if (hour.toString().length === 1){
        hour = '0' + hour;
    }
    if (minute.toString().length === 1){
        minute = '0' + minute;
    }
    if (seconds.toString().length === 1){
        seconds = '0' + seconds;
    }


    const timeDiv = document.getElementById('time-display');
    timeDiv.innerHTML = hour + ':' + minute + ':' + seconds + ' ' + period;
    document.body.appendChild(timeDiv);
}

setInterval(setTime, 1000);
jwhunt1
  • 83
  • 3
  • 3
    The commented out function doesn't return a value. It just changes the **local** variable that is passed into it. – Taplar May 15 '20 at 22:39
  • This is because of the Scope, as someone mentions it before. You are change the value of `timeValue` inside the `twoDigit` method, but not outside of it. For this what you have to do is inside the twoDigit method add `return '0' + timeValue;` and set the seconds, minutes or hours to the value of the result of twoDigit like this `seconds = twoDigits(seconds)` – Paul Miranda May 15 '20 at 23:17
  • https://jsbin.com/caboqixuva/edit?html,js,output There's nothing incorrect with what I said. The question was closed because this is a scope issue. The variable on a function parameter is not the same variable as what was provided to the function call. Thus in the case that it points to an immutable value, and you change what that variable points to, the original will not be modified. To modify the original, you *must* return the modification and store it back in the original variable. Scope rules do not do this for you automatically. This is all covered in the duplicate. – Taplar May 15 '20 at 23:18
  • Yes, when you put in a single digit number and test it that way it does work. In the context of my code and question posted however, that does not work. – jwhunt1 May 15 '20 at 23:21
  • In what case does it not work? "Not work" referring to the fix of the function not behaving the same way that the non-function version would also work – Taplar May 15 '20 at 23:22
  • I see now. Thank you. I do however stand my ground that this platform is so inhuman to the newbie developer. Why could you have no said all that from the beginning. It appears that I got it to work now by, like you said, storing the value once returned or rather calling the function like so, seconds = twoDigit(seconds); minute = twoDigit(minute); hour = twoDigit(hour); – jwhunt1 May 15 '20 at 23:26
  • The point of closing questions as duplicates is because we are all human, and we all have our own jobs to do. We're trying to help, but our time is not endless. Rather than repeating this answer over and over and over for every question that asks ruffly the same thing, we close as a duplicate, because all the information we would otherwise repeat over and over, is already detailed else where. – Taplar May 15 '20 at 23:28
  • My returned frustration was because that duplicate was not clicking with me, but your sentence "To modify the original, you must return the modification and store it back in the original variable" made the answer all too clear. More human sentences are needed on this platform. I understand your point, I hope you can understand why answers and thread closures like this make newbies like myself feel stranded and irritated. I apologize for any rudeness and thank you for your time and assistance. – jwhunt1 May 15 '20 at 23:42

0 Answers0