Exercise 2 from Eloquent JavaScript
//Problem: Print out all numbers from 1 to 100. When divisible by 3 print Fizz, then divisible by 5 print Buzz, when divisible by both 3 and 5 print FizzBuzz // Solution: JS - Loop through all the numbers, check modulo 3, 5 and 3 and 5, print appropriate word function fizzBuzz(min, max) { function fizzOrBuzz (number, divisorA, divisorB) { if (divisorB) { return ((number % divisorA === 0) && (number % divisorB === 0)) } else { return number % divisorA === 0 } } for (var i = min; i <= max; i++) { if (fizzOrBuzz(i, 3, 5)) { console.log('FizzBuzz (', i , ')'); } else if (fizzOrBuzz(i, 3)) { console.log('Fizz (', i, ')'); } else if (fizzOrBuzz(i, 5)) { console.log('Buzz (', i , ')'); } else { console.log(i); } } } fizzBuzz(1, 100);
Exercise 3 from Eloquent JavaScript
// Problem: Draw out a chessboard of 8*8 using the space and # characters, alternating the start character. // Solution: JS - Use a loop to draw out the columns then at the end, alternate a boolean to start with one character or the other. function chessBoard(spread) { var blank = false; var a = ' ', b = '#', row = ''; for (var c = 1; c <= spread*spread; c++) { if (c % spread === 0) { row += '\n' } else if(blank) { row += a; blank = !blank; } else { row += b blank = !blank; } } console.log(row); } chessBoard(8);
Exercise 4 from Eloquent JavaScript
// Problem: Return the minimum number between two numbers // Solution: Accept two arguments and determime which is less function min(a, b) { if (typeof(a) === 'number' && typeof(b) === 'number') { var min = a < b; return min ? a : b } else { return NaN; } } console.log(min(3,-9));
Exercise 5 from Eloquent JavaScript
// Problem: Use recursion to determine whether a number is even, if even, return true // Solution: Create a function that returns true if number is 0, false if number is 1, else subtract 2 from the number function isEven(number){ if(number < 0) { number = number * -1; } if (number === 0) { return true; } else if (number === 1) { return false; } else { number -= 2; return isEven(number); } } console.log(isEven(-34));
Exercise 6 from Eloquent JavaScript
// Problem: Create a function that counts how many times a provided letter appears in a string // Solution: Loop through the string using .charAt to determine whether the letter is a match function countChars(string, letter){ var count = 0; for (var i = 0; i < string.length; i++) { if (string.charAt(i) === letter) { count+=1; } } return count; } console.log(countChars('chakkalakka', 'a'));
Calculate prime numbers in a range
function getPrimes(min, max) { function isPrimeBruteForce(number){ for (var i = 2; i < (number - 1); i++) { if (number % i === 0) { return false; } } return true } if (min < max && min > 1 && max > 1) { var primes = []; for (var i = min; i < max; i++) { if(isPrimeBruteForce(i)) { primes.push(i); } } return primes; } else { return -1; } } console.log(getPrimes(3, 100));
Exercise 7 from Eloquent JavaScript
function range(start, end, increment) { var arr = []; var inc = 1; if (increment) { inc = increment; } for (var i = start; i <= end; i+=inc) { arr.push(i); } return arr; } function sum(array) { var sum = 0; for (var i = 0; i < array.length; i++) { sum = sum + array[i]; } return sum; } console.log(sum(range(1,10, 2)));