-
Summation to
n: Let's implement the functionsumthat takes a single parametern, and computes the sum of all integers up tonstarting from0, e.g.:function sum(n) { // TODO: your code here } sum(3); // => 6 sum(4); // => 10 sum(5); // => 15
-
Factorial of
n: The factorial ofnis the product of all the integers precedingn, starting with1, e.g.:function factorial(n) { // TODO: your code here } factorial(3); // => 6 factorial(4); // => 24 factorial(5); // => 120
-
Repeating a String
nTimes: Let's write a function calledrepeatStringthat takes two parameters: a stringstr, which is the string to be repeated, andcount-- a number representing how many times the stringsshould be repeated, e.g.function repeatString(str, count) { // TODO: your code here } repeatString('dog', 0); // => '' repeatString('dog', 1); // => 'dog' repeatString('dog', 2); // => 'dogdog' repeatString('dog', 3); // => 'dogdogdog'
Your task is to implement the
repeatStringfunction using awhileloop.
-
Modify your
sumfunction from the Basic Requirements section to accept two parameters,startandend:sumshould now compute the sum of the numbers fromstarttoend, e.g.function sum(start, end) { // TODO: your code here } sum(2, 7); // => 2 + 3 + 4 + 5 + 6 + 7 => 27 sum(3, 5); // => 3 + 4 + 5 => 12
- What happens if
startis larger thanend? Modifysumto check for this case and, when found, swap thestartandendarguments.
- What happens if
-
Let's pretend for a moment that JavaScript does not have the addition operator
+-- instead, it comes with two functions calledincanddecthat perform increment and decrement respectively:// ignore the fact that inc makes use of + function inc(x) { return x + 1; } function dec(x) { return x - 1; }
Your task is to write a function called
addthat takes two numbers as parameters,xandy, and adds them together. The catch is that you can only useincanddecto accomplish this. -
Write a function called
isEventhat, given a numbernas a parameter, returnstrueif that number is even, andfalseotherwise; however, you need to do this without using the%operator. -
Write a function called
multiplythat accepts two numbers as parameters, and multiplies them together -- but without using the*operator; instead, you'll need to use repeated addition.
-
Compute the
nth Fibonacci Number: The fibonacci numbers are represented by the following sequence:// fib(n): 1 1 2 3 5 8 13 21 // | | | | | | | | // n: 0 1 2 3 4 5 6 7
That is,
fib(0)is 1,fib(1)is 1,fib(2)is 2,fib(3)is 3,fib(4)is 5, etc.Notice that each fibonacci number can be computed by adding the previous two fibonacci numbers, with the exception of the first two:
fib(0)andfib(1). More succinctly,fib(0)is1fib(1)is1fib(n)isfib(n - 1) + fib(n - 2)
Write a function called
fibthat accepts a numbernas a parameter and computes thenth fibonacci number using the above rules. -
By now you should have worked with the
lengthproperty of strings, e.g."hello".length. Your task is to write a function calledstringLengththat accepts a string as a parameter and computes the length of that string; however, as you may have guessed, you are not allowed to use thelengthproperty of the string!Instead, you'll need to make use of the string method called
slice. To get an idea of howsliceworks, try the following at a console:"hello".slice(0); "hello".slice(1); "".slice(1);
For our purposes, we can consider
sliceas taking one argument -- the index to begin slicing from, and returns a new string starting from that index onwards.Indices are positions of characters within strings, and they always begin counting from 0, e.g.:
// "h e l l o" (spaces added for clarity) // | | | | | // 0 1 2 3 4
The
"h"character has index (position)0in the string"hello","e"has index1,lhas index2, etc. -
The "modulo" operator (
%) computes the remainder after dividing its left operand by its right one, e.g.5 % 2; // => 1 8 % 10; // => 8 7 % 5; // => 2
Write a function called
modulothat works like the%operator, but without using it. -
Write a function called
countCharsthat accepts two parameters: astringand acharacter. This function should return a number representing the number of times that thecharacterappears instring. To access the first element of a string, you can use the following syntax:// access the element at index 0 "hello"[0]; // => "h" "dog"[0]; // => "d"
HINT: You'll also need to make use of the
slicemethod as shown above in the exercise on computing the length of a string. -
Implement a function called
indexOfthat accepts two paramters: astringand acharacter, and returns the first index ofcharacterin thestring. You'll need to make use of the techniques for accessing the first element of a string and the rest of the string (slice) as before.