Skip to content

Instantly share code, notes, and snippets.

@HerbCaudill
Last active June 11, 2020 12:57
Show Gist options
  • Save HerbCaudill/4a531ebc144f9b37b99866b2d4d9b457 to your computer and use it in GitHub Desktop.
Save HerbCaudill/4a531ebc144f9b37b99866b2d4d9b457 to your computer and use it in GitHub Desktop.
Coding Adelante: Challenges

Coding Adelante: Challenges

Hello, world!

  1. Write a function called hello that prints "Hello, world!" in the console.

    function hello() {
        ...
    }
        
    hello()
    //  Hello, world!
  2. Write a function that takes a name and prints "Hello, " followed by the name:

    function hello(name) { ... }
        
    hello('Herb')
    //  Hello, Herb!
  3. Write a function that takes two names and says hello to both of them:

    function hello(name1, name2) { ... }
    
    hello('Herb', 'Georgi')
    // Hello, Herb and Georgi!
  4. Write a function that takes one or two names. If two names are provided, say hello to both of them. Otherwise, say hello to just one.

    function hello(name1, name2) {...}
    
    hello('Herb')
    // Hello, Herb!
    
    hello('Herb', 'Georgi')
    // Hello, Herb and Georgi!                              

Functions with numbers

arrays, loops

  1. Write a function that takes two numbers a and b and multiplies them together.

    function multiply(a, b) { ... }
    
    multiply(3, 4)
    // 12
  2. Write a function that takes an array of numbers and adds them all up.

    function sum(arr) { ... }
    
    sum([234, 59253, 12])
    // 59499
  3. Write a function that takes an array of numbers and returns the average.

    function average(arr) { ... }
    
    average([5, 7, 12])
    // 8
  4. Write a function that takes an array of numbers and returns the largest number.

    function max(arr) { ... }
    
    max([17, 24, 32, 5, 155, 77])
    // 155

Even/odd

  1. Write a function named isEven that takes a number and returns true if it is even, false if it is odd.

    function isEven(n) { ... }
    
    isEven(5)
    // false
    isEven(12423578)
    // true
  2. Write a function isOdd that takes a number and returns true if the number is odd, false if it is even. Use the isEven function inside isOdd.

  3. Write a function named allAreEven that takes an array of numbers and returns true if all the numbers in the array are even.

  4. Write a function named someAreEven that takes an array of numbers and returns true if at least one of the numbers in the array are even.

  5. Write a function named evenPercentage that takes an array of numbers and returns the percentage of numbers in the array that are even.

Divisibility

  1. Write a function that tells if a number is divisible by 3.

    function isDivisibleBy3(n) { ... }
    
    isDivisibleBy3(27) 
    // true
    isDivisibleBy3(25)
    // false
  2. Write a function that tells if a number is divisible by 5.

  3. Write a function that takes two numbers, n and d, and tells if n is divisible by d.

    function isDivisible(n, d) { ... }
    
    isDivisible(27, 3)
    // true
    isDivisible(25, 5)
    // true
    isDivisible(25, 3)
    // false
  4. Refactor isDivisibleBy3 and isDivisibleBy5 so that they use the general isDivisible function internally.

Objects

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment