-
Write a function called
hello
that prints "Hello, world!" in the console.function hello() { ... } hello() // Hello, world!
-
Write a function that takes a name and prints "Hello, " followed by the name:
function hello(name) { ... } hello('Herb') // Hello, Herb!
-
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!
-
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!
arrays, loops
-
Write a function that takes two numbers
a
andb
and multiplies them together.function multiply(a, b) { ... } multiply(3, 4) // 12
-
Write a function that takes an array of numbers and adds them all up.
function sum(arr) { ... } sum([234, 59253, 12]) // 59499
-
Write a function that takes an array of numbers and returns the average.
function average(arr) { ... } average([5, 7, 12]) // 8
-
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
-
Write a function named
isEven
that takes a number and returnstrue
if it is even,false
if it is odd.function isEven(n) { ... } isEven(5) // false isEven(12423578) // true
-
Write a function
isOdd
that takes a number and returnstrue
if the number is odd,false
if it is even. Use theisEven
function insideisOdd
. -
Write a function named
allAreEven
that takes an array of numbers and returnstrue
if all the numbers in the array are even. -
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. -
Write a function named
evenPercentage
that takes an array of numbers and returns the percentage of numbers in the array that are even.
-
Write a function that tells if a number is divisible by 3.
function isDivisibleBy3(n) { ... } isDivisibleBy3(27) // true isDivisibleBy3(25) // false
-
Write a function that tells if a number is divisible by 5.
-
Write a function that takes two numbers,
n
andd
, and tells ifn
is divisible byd
.function isDivisible(n, d) { ... } isDivisible(27, 3) // true isDivisible(25, 5) // true isDivisible(25, 3) // false
-
Refactor
isDivisibleBy3
andisDivisibleBy5
so that they use the generalisDivisible
function internally.