diff --git a/.gitignore b/.gitignore index 3c3629e..f6a921b 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,2 @@ node_modules +.vscode/ \ No newline at end of file diff --git a/exercises.js b/exercises.js index 6364db9..31156ca 100644 --- a/exercises.js +++ b/exercises.js @@ -3,6 +3,11 @@ function getBiggest(x, y) { //x and y are integers. Return the larger integer //if they are the same return either one + if (x >= y) { + return x; + } else { + return y; + } } function greeting(language) { @@ -11,15 +16,34 @@ function greeting(language) { //language: 'English' -> 'Hello!' //language: 'Spanish' -> 'Hola!' //if language is undefined return 'Hello!' + if (language === 'German') { + return 'Guten Tag!'; + } else if (language === 'English') { + return 'Hello!'; + } else if (language === 'Spanish') { + return 'Hola!'; + } else { + return 'Hello!'; + } } function isTenOrFive(num) { //return true if num is 10 or 5 //otherwise return false + if (num === 10 || num === 5) { + return true; + } else { + return false; + } } function isInRange(num) { //return true if num is less than 50 and greater than 20 + if (num < 50 && num > 20) { + return true; + } else { + return false; + } } function isInteger(num) { @@ -29,6 +53,12 @@ function isInteger(num) { //-10 -> true //otherwise return false //hint: you can solve this using Math.floor + if (num === Math.floor(num)) { + return true; + } else { + return false; + } + } function fizzBuzz(num) { @@ -36,6 +66,15 @@ function fizzBuzz(num) { //if num is divisible by 5 return 'buzz' //if num is divisible by 3 & 5 return 'fizzbuzz' //otherwise return num + if (num % 3 === 0 && num % 5 === 0) { + return 'fizzbuzz'; + } else if (num % 3 === 0) { + return 'fizz'; + } else if (num % 5 === 0) { + return 'buzz'; + } else { + return num; + } } function isPrime(num) { @@ -44,35 +83,57 @@ function isPrime(num) { //hint: a prime number is only evenly divisible by itself and 1 //hint2: you can solve this using a for loop //note: 0 and 1 are NOT considered prime numbers + if (num > 1) { + for (var i = 2; i < num; i++) { + if (num % i === 0) { + return false; + } + } + return true; + } else { + return false; + } + } function returnFirst(arr) { //return the first item from the array + return arr[0]; } function returnLast(arr) { //return the last item of the array + return arr[arr.length - 1]; } function getArrayLength(arr) { //return the length of the array + return arr.length; } function incrementByOne(arr) { //arr is an array of integers //increase each integer by one //return the array + for (var i = 0; i < arr.length; i++) { + arr[i]++; + } + return arr; } function addItemToArray(arr, item) { //add the item to the end of the array //return the array + arr.push(item); + return arr; } function addItemToFront(arr, item) { //add the item to the front of the array //return the array //hint: use the array method .unshift + arr.unshift(item); + return arr; } function wordsToSentence(words) { @@ -80,26 +141,45 @@ function wordsToSentence(words) { //return a string that is all of the words concatenated together //spaces need to be between each word //example: ['Hello', 'world!'] -> 'Hello world!' + return words.join(' '); } function contains(arr, item) { //check to see if item is inside of arr //return true if it is, otherwise return false + return arr.indexOf(item) > -1; } function addNumbers(numbers) { //numbers is an array of integers. //add all of the integers and return the value + var total = 0; + for (var i in numbers) { + total += numbers[i]; + } + return total; } function averageTestScore(testScores) { //testScores is an array. Iterate over testScores and compute the average. //return the average + var total = 0; + for (var i in testScores) { + total += testScores[i]; + } + return total / testScores.length; } function largestNumber(numbers) { //numbers is an array of integers //return the largest integer + var greatest = numbers[0]; + for (var i = 0; i < numbers.length; i++) { + if (greatest <= numbers[i]) { + greatest = numbers[i]; + } + } + return greatest; } //Do not modify code below this line. @@ -124,4 +204,4 @@ module.exports = { addNumbers: addNumbers, averageTestScore: averageTestScore, largestNumber: largestNumber -}; +}; \ No newline at end of file