GithubHelp home page GithubHelp logo

javascript-basics-challenge's Introduction

JavaScript Basics Challenge

In this challenge, you'll deepen your JavaScript fundamentals by completing a series of exercises. You'll need to follow the following steps to successfully complete this challenge:

  • Fork this repository
  • As you work through each section below, add a file (named according to what exercise you're on).
  • Complete the exercise by writing your JS code and corresponding unit tests.
  • When you finish, create a Pull Request back to this repository with your completed challenge!

1. Rectangles

You'll write a number of functions that calculate certain properies of a rectangle object. A rectangle object is just a JavaScript object with two properties - a width and a height. The input will look like this:

var rectangle = {width: 10, height: 20}

You're expected to write the following functions:

  1. area(rectangle) - returns the area of the rectangle
  2. perimeter(rectangle) - returns the perimeter of the rectangle
  3. diagonal(rectangle) - returns the length of the rectangle's diagonal
  4. isSquare(rectangle) - returns true if the rectangle is a square and false otherwise

Here's a resource on rectangle diagonals.

Don't forget to write tests for each function!

2. Triangles

Write a function called isTriangle which takes an input of three non-negative numbers. It should return true if the three numbers could form the side lengths of a triangle and false otherwise.

The arguments don't correspond to specific sides. Be sure to handle edge cases such as negative numbers as input.

For example:

isTriangle(0,0,0) # => false, because a triangle can't have 0 length sides
isTriangle(-2,1,5) # => false, because a triangle can't have negative length sides

isTriangle(4,4,4) # => true, an equilateral triangle

isTriangle(6,8,10) # => true, a right triangle
isTriangle(8,6,10) # => true, the same right triangle
isTriangle(10,8,6) # => true, the same right triangle

isTriangle(4, 8, 50) #=> false, no such triangle exists

It might be helpful to read this Wikipedia article about Pythagorean triples.

3. Averages

Add four instance methods to Javascript's Array:

  1. total - returns the total of all the values in the array
  2. mean - returns the mean (average) of the array
  3. median - returns the median of the array
  4. mode- returns an object representing the mode(s) of the array with the property being the mode and the value being the frequency

Note: You'll need to research extending built-in JavaScript objects. Generally, we want to avoid extending JavaScript's built in classes, but this is just an exercise.

4. Count Numbers Between

Write a function countNumbersBetween which takes three arguments as input:

  1. An Array of integers
  2. An integer lower bound
  3. An integer upper bound

countNumbersBetween should return the number of integers in the Array that are between the two bounds, including the bounds.

It should return 0 if the Array is empty.

Some examples:

countNumbersBetween([25, 2, 8], 0, 100);          // => 3
countNumbersBetween([-19, 1, 22], 0, 100);        // => 2
countNumbersBetween([100, 200, 300], 100, 300);   // => 3
countNumbersBetween([], -10, 10);                 // => 0
countNumbersBetween([5], 5, 5);                   // => 1

5. Factorial

Write a factorial function which takes as its input a non-negative integer and calculates the factorial of that number.

The factorial of a number is the product of all integers from 1 up to that number. For example:

factorial(4) // 24 (4 * 3 * 2 * 1 == 24)

The factorial of 0 is defined to be 1.

Read the following Wikipedia article for more information.

6. Longest String

Write a function longestString which takes as its input an Array of Strings and returns the longest String in the Array.

For example:

longestString(['horses', 'cats', 'pineapples']); // => "pineapples"

If the input Array is empty longestString should return null.

7. Times Table

Implement a function called timesTable which takes as its input an integer representing a number of rows and produces a string representing a times table with that number of rows.

The numbers can be separated by any spaces or tabs, but each row must be on a new line. Don't worry if the columns don't line up.

For example, timesTable(5) should print the following out to the screen:

1  2  3  4  5
2  4  6  8  10
3  6  9  12 15
4  8  12 16 20
5  10 15 20 25

8. Guessing Game

Create a GuessingGame constructor function which is initialized with an integer called answer.

Define an instance method guess which takes an integer called guess as its input. guess should return the string 'high' if the guess is larger than the answer, 'correct' if the guess is equal to the answer, and 'low' if the guess is lower than the answer.

Define an instance method isSolved which returns true if the most recent guess was correct and false otherwise.

For example:

var game = new GuessingGame(20);

game.isSolved();   // => false

game.guess(5);     // => 'low'
game.guess(40);    // => 'high'
game.isSolved();   // => false

game.guess(20);    // => 'correct'
game.isSolved()    // => true

javascript-basics-challenge's People

Contributors

case-eee avatar caward12 avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.