Skip to main content

Examples

Below are a couple of examples for how you should go about this assignment.

Example 1:โ€‹

Setupโ€‹

  1. Navigate to your repository in the command line.
  • You can either use the cd PATH_TO_FOLDER command.
    • in my case the command was cd /Users/wesreed/git/School/IT3049/2.JS-Exercise
    • use the pwd command to Print Working Directory and confirm your location
  • OR You could open VSCode integrated terminal window and it will open in the terminal already in the right path.
  1. Installing the dependencies npm install

  2. Run the tests using npm test.

    example image

  • notice all the errors, our goal for this assignment is to resolve them all.

Getting Down to Businessโ€‹

  1. Open the project in Visual Studio Code (Not Visual Studio)

    example image

    Let's try to solve a couple of those tests.

  2. Open the file src/1.numbers.js. Particularly

      //parseInt: should use parseInt correctly to convert strings to integer numbers
    function parseInt (str) {
    return ;
    }
    • notice the function have an empty implementation. - We need to do something about that ๐Ÿค”
  3. Consulting the JS documentation for the Number Data Type, you learn that there's a function named Number.parseInt(). The documentations shows usage examples, and specifies the parameters and return type of the function.

    Number.parseInt() docs

    Key Takeaways:

    • Be careful the Radix does not default to 10!, so we'll have to set that optional parameter as follows
        function parseInt(str) {
      return Number.parseInt(str, 10);
      }
    • Don't forget to return something from the function.
  4. Re-run the tests again and let's see if it passes

    1 passing test
  5. commit your code commit logo git commit -m "finished test parseInt".


Example 2:โ€‹

  1. Well, no need for the setup steps here (you should already be in the directory)
  2. Open the file src/3.arrays.js. Particularly at this function
      /**
    * IndexOf: you should be able to determine the location of an item in an array
    * Example:
    * arr = [1,2,3,5,6]
    * calling the function like
    * indexoff(arr, 3); should return the index 2
    */
    function indexOf(arr, item) {
    return ;
    }
  3. The indexOf method is supposed to return the index of a certain element in an array. You can learn more about JS Built-in method here
  4. my implementation for this is as follows
      function indexOf(arr, item) {
    return arr.indexOf(item);
    }
  5. Re-run the tests again and let's see if it passes
  6. commit your code commit logo git commit -m "finished test: IndexOf".

On to the next test .. Rinse ๐Ÿงผ and Repeat ๐Ÿ”