Sumrange JavaScript exercise
const sumRange = () => {
let array = [];
function range(arr) {
let lowNumber = Math.min(arr[0], arr[1]);
let highNumber = Math.max(arr[0], arr[1]);
for (i = lowNumber; i <= highNumber; i++) {
array.push(i);
}
return array;
}
function sum(array) {
let total = 0;
for (i = 0; i < array.length; i++) {
total = total+array[i];
}
return total;
}
console.log(sum(range([1, 10])));
}
sumRange();
Factorial exercise
function factorial(n) {
if (n == 0) {
return 1;
} else {
return factorial(n - 1) * n;
}
}
console.log(factorial(8));
// → 40320
Chapter 2 exercises - looping a triangle
let hashTag = '';
for (let i = 0; i < 7; i++) {
hashTag += '#';
console.log(hashTag);
}
Chapter 2 exercises - FizzBuzz
let numberCount = 0;
for (let i = 0; i < 100; i++) {
numberCount += 1;
if (numberCount % 3 === 0 && numberCount % 5 == 0) {
console.log('FizzBuzz');
} else if (numberCount % 3 === 0) {
console.log('Fizz');
} else if (numberCount % 5 === 0) {
console.log('Buzz');
} else {
console.log(numberCount);
}
}
Chapter 2 exercises - Chessboard
let board =
' ' + '#' + ' ' + '#' + ' ' + '#' + ' ' + '#' + '\n'
+ '#' + ' ' + '#' + ' ' + '#' + ' ' + '#' + ' ' + '\n'
+ ' ' + '#' + ' ' + '#' + ' ' + '#' + ' ' + '#' + '\n'
+ '#' + ' ' + '#' + ' ' + '#' + ' ' + '#' + ' ' + '\n'
+ ' ' + '#' + ' ' + '#' + ' ' + '#' + ' ' + '#' + '\n'
+ '#' + ' ' + '#' + ' ' + '#' + ' ' + '#' + ' ' + '\n'
+ ' ' + '#' + ' ' + '#' + ' ' + '#' + ' ' + '#' + '\n'
+ '#' + ' ' + '#' + ' ' + '#' + ' ' + '#' + ' ' + '\n';
let chessBoard = '';
let size = 8;
for (let i = 1; i <= size; i++) {
if (i % 2 === 0) {
for (j = 1; j <= size; j++) {
if (j % 2 === 0) {
chessBoard += ' ';
} else {
chessBoard += '#';
}
if (j === size) {
chessBoard += '\n';
}
}
} else {
for (j = 1; j <= size; j++) {
if (j % 2 === 0) {
chessBoard += '#';
} else {
chessBoard += ' ';
}
if (j === size) {
chessBoard += '\n';
}
}
}
if (i === size) {
console.log(chessBoard);
}
}
Chapter 3 exercises - Minimum
const min = (a, b) => {
if (a > b) {
return b;
} else if (b > a) {
return a;
} else {
return 'both numbers are the same';
}
}
console.log(min(0, 10));
// → 0
console.log(min(0, -10));
// → -10
Chapter 3 exercises - Recursion
const isEven = (a) => {
if (a === 0) {
return true;
} else if (a === 1) {
return false;
} else if (a < 0) {
return isEven(-a);
} else {
return isEven(a - 2);
}
}
console.log(isEven(50));
// → true
console.log(isEven(75));
// → false
console.log(isEven(-1));
// → false
Chapter 3 exercises - Bean counting
const countBs = (a) => {
let upperCaseB = 0;
for (let i = 0; i < a.length; i++)
if (a[i].toUpperCase() === 'B') {
upperCaseB += 1;
}
return upperCaseB;
}
const countChar = (a, b) => {
let charNum = 0;
for (let i = 0; i < a.length; i++) {
if (a[i] === 'k') {
charNum += 1;
}
}
let charString =
"There are " + charNum + " " + b + " characters.";
return charString;
}
console.log(countBs("BBC"));
// → 2
console.log(countChar("kakkerlak", "k"));
// → There are 4 k characters.
Chapter 4 exercises - The sum of a range
function range(start, end, step = start < end ? 1 : -1) {
let array = [];
if (step > 0) {
for (let i = start; i <= end; i += step) array.push(i);
} else {
for (let i = start; i >= end; i += step) array.push(i);
}
return array;
}
function sum(array) {
let total = 0;
for (let value of array) {
total += value;
}
return total;
}
console.log(range(1, 10));
console.log(range(5, 2, -1));
console.log(sum(range(1, 10)));
Chapter 4 exercises - Reversing an array
function reverseArray(array) {
let array2 = [];
for (let i = array.length - 1; i >= 0; i--) {
array2.push(array[i]);
}
return array2;
}
function reverseArrayInPlace(array) {
for (let i = 0; i < Math.floor(array.length/2); i++) {
let old = array[i];
array[i] = array[array.length - 1 - i];
array[array.length - 1 - i] = old;
}
return array;
}
let arrayValue = [7, 2, 3, 4, 5, 6, 7];
let arrayLetters = ["A", "B", "C", "D", "E", "F"];
let arrayCopy = arrayLetters.slice(0);
reverseArrayInPlace(arrayValue);
console.log(arrayValue);
console.log(arrayLetters);
console.log(arrayCopy);
console.log(reverseArray(arrayLetters));
Chapter 4 exercises - A list
function arrayToList(array) {
let list = {};
let rest = {};
for (let i = array.length -1; i >= 0 ; i--) {
rest.value = array[i];
if (i === array.length -1) {
rest.rest = null;
} else {
rest.rest = list;
}
list = {...rest};
}
return list;
}
console.log(arrayToList([10, 20, 30]));
// → {value: 10,
rest: {
value: 20,
rest: {
value: 30,
rest: null}}}
function listToArray(list) {
let listTwo = list;
const array = [];
while (true) {
array.push(listTwo.value);
if (listTwo.rest === null) {
break;
}
listTwo = listTwo.rest;
}
return array;
}
console.log(listToArray(arrayToList([10, 20, 30])))
// -> [10, 20, 30]
function prepend(e, list) {
let newList = {value: e, rest: list}
return newList;
}
console.log(prepend(10, prepend(20, prepend(30, null))));
// → {value: 10,
rest: {
value: 20,
rest: {
value: 30,
rest: null}}}
function nth(list, number) {
let newList = list;
const item = [];
let itemFound;
console.log(list);
if (!list) {
return undefined;
}
while (true) {
item.push(newList.value)
if (newList.rest === null) {
break;
}
newList = newList.rest;
}
for (let i = 0; i < item.length; i++) {
if (i === number) {
itemFound = item[i];
}
}
return itemFound;
}
console.log(nth(arrayToList([10, 20, 30]), 1));
// → 20