Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Homework 2 by JohanCHM #5

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
node_modules
.vscode/
82 changes: 81 additions & 1 deletion exercises.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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) {
Expand All @@ -29,13 +53,28 @@ 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) {
//if num is divisible by 3 return 'fizz'
//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) {
Expand All @@ -44,62 +83,103 @@ 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) {
//words is an array of strings
//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.
Expand All @@ -124,4 +204,4 @@ module.exports = {
addNumbers: addNumbers,
averageTestScore: averageTestScore,
largestNumber: largestNumber
};
};