Jump to content

DanhBui

New Members
  • Posts

    3
  • Joined

  • Last visited

Posts posted by DanhBui

  1. Hey everybody, I'm a beginner at JavaScript so there are many things I don't understand. I'd appreciate very much if you can help me figuring out something new.

    I'm trying to solve this problem on Checkio.org called "The Most Numbers". The requirement is quite simple: It is given an array of floats and you have to calculate the difference between the maximum and minimum numbers. And this is my solution (which didn't work):

    function mostNumbers(numbers){
        if (numbers.length > 0) {
            return Math.max(numbers) - Math.min(numbers);
        } else {
            return 0;}
    }

    It produced "0" regardless of what "numbers" is.

    I tried some stuff related to the Math.max() and Math.min() methods. And this is what I received:

    Math.max(1,2,3) // => 3

    But when I assigned the array to a variable, this is what I received:

    var numbers = (1,2,3);
    Math.max(numbers) // => 3 (nothing wrong)
    Math.min(numbers) // => 3 (why?)

    I realized that it always take the last number, regardless what I want, so I tried typing (1,2,3) into the console and I got 3. Why is that? Then I realized that this is not a normal array, which has the structure [something, something else,...].

    I reached out to w3school but what I received are just simple examples like

    Math.max(5, 10); // => 10

    I reached out to stackoverflows as well and found this:

    image.png.c37aa3482e09d5c75549e02721ceb03b.png

    With all the respect for this person (who is contributing value, that's great), I find the problem even more confusing.

    - What is a series of numbers, if not an array of numbers?

    - What is a spread operator? I clicked on the link he/she provided, but I didn't understand what I read:

    Quote

    Spread syntax allows an iterable such as an array expression or string to be expanded in places where zero or more arguments (for function calls) or elements (for array literals) are expected, or an object expression to be expanded in places where zero or more key-value pairs (for object literals) are expected. (https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Spread_operator)

    Thank you for reading my post, I know it's long. I appreciate it.

  2. Hi Stef, I'm very happy that you answered me directly. But I think you misunderstood what I meant because I didn't say it very clearly. I watched you reseting the value of the button in Chapter 7 - lesson 3 of the course and this is what happened when I run your code:

    At first, the text in the button was "Do it again!"

    image.png.a4c3cd55494432cafb39ab87c19b792f.png

    After I clicked on the button, in turned into "Done looping!" like this:

    image.png.ee316994c55f32a57b15a7bbccd46e71.png

    But I knew what I did wrong. I just took a good look at your code and found out that you used "input", not "button", and you set the value of the "input" tag to "Do it again!".

    What I did was using the "button" tag and then set the text to "Do it!" and then reset its value, what didn't exist. Even if it did existed, it couldn't appear on the surface of the button. I tried again and got the result I wanted!

    image.png.e5b08fcab9cc9f864853bf850095fcff.png => image.png.d8ee350162036bbe5f23156947bed350.png

    Sorry for the "alert" thing. I just put it there with no reason.

  3. Hello everybody, I'm taking Stef's "Beginners JavaScript" course and saw how he set different text that appears inside the buttons. However when I tried it myself, nothing happened. I've checked many times but still couldn't find out what I did wrong. Below is my code, please help me fix it. Thank you very much!

    - Here's the image version (to make the code less boring and easier to read compared to the text version)

    • Here's the code in my html file:

    image.png.3d00442178698f8c9c149c7052ee36b5.png

    • Here's the code in my external js file:

    image.png.8f3378fdbcc12b01002e9978f6a38c33.png

    image.png.0ca561d8a792c6f834701857a1141108.png

    - And here's the text version in case you need to copy:

    <button id="functionExperiment">Do it!</button>

    function functionExperiment () {
        var functionEx = document.getElementById("functionExperiment");
        functionEx.value = "Oh Yeah!";
        alert(document.title);
    }

    document.getElementById("functionExperiment").onclick = function () {
        functionExperiment ();
    }

×
×
  • Create New...