Hack Reactor: Module 1

¡Supera tus tareas y exámenes ahora con Quizwiz!

____________ _______________are string or symbol. Any other value, including a number, is coerced to a string. This outputs 'value', since 1 is coerced into '1'.

Property names

Write a function called "findMaxLengthOfThreeWords". Given 3 words, "findMaxLengthOfThreeWords" returns the length of the longest word. var output = findMaxLengthOfThreeWords('a', 'be', 'see'); console.log(output); // --> 3 function findMaxLengthOfThreeWords(word1, word2, word3) { }

function findMaxLengthOfThreeWords(word1, word2, word3) { if (word1.length > word2.length) { return word1.length; } else if (word2.length > word3.length) { return word2.length; } else { return word3.length; } }

Write a function called "findMinLengthOfThreeWords". Given 3 words, "findMinLengthOfThreeWords" returns the length of the shortest word. var output = findMinLengthOfThreeWords('a', 'be', 'see'); console.log(output); // --> 1 function findMinLengthOfThreeWords(word1, word2, word3) { }

function findMinLengthOfThreeWords(word1, word2, word3) { if (word1.length < word2.length) { return word1.length; } else if (word2.length < word3.length) { return word2.length; } else { return word3.length; } }

Write a function called "findShortestElement". Given an array, "findShortestElement" returns the shortest string within the given array. Notes: - If there are ties, it should return the first element to appear. - If the given array is empty, it should return an empty string. var output = findShortestElement(['a', 'two', 'three']); console.log(output); // --> 'a' function findShortestElement(arr) { }

function findShortestElement(arr) { if (arr.length === 0) { return ''; } var shortestElement = arr[0]; for (var i = 0; i < arr.length; i++) { if (arr[i].length < shortestElement.length){ shortestElement = arr[i]; } } return shortestElement }

Write a function called "addObjectProperty". Given two objects and a key, "addObjectProperty" sets a new property on the 1st object at the given key. The value of that new property is the entire 2nd object. var person1 = { name: 'Joe Blow', role: 'schlub' }; var person2 = { name: 'Mr. Burns', role: 'supervisor' }; addObjectProperty(person1, 'manager', person2); console.log(person1.manager); // --> { name: 'Mr. Burns', role: 'supervisor' } function addObjectProperty(obj1, key, obj2) { }

function addObjectProperty(obj1, key, obj2) { obj1[key] = obj2; }

Write a function called "addProperty". Given an object, and a key, "addProperty" sets a new property on the given object with a value of true. var myObj = {}; addProperty(myObj, 'myProperty'); console.log(myObj.myProperty); // --> true function addProperty(obj, key) { }

function addProperty(obj, key) { obj[key] = true; }

Write a function called "addToBack". Given an array and an element, "addToBack" returns the given array with the given element added to the end. Notes: It should be the SAME array, not a new array. var output = addToBack([1, 2], 3); console.log(output); // -> [1, 2, 3] function addToBack(arr, element) { }

function addToBack(arr, element) { arr.push(element); return arr; }

Write a function called "addToBackNew". Given an array and an element, "addToBackNew" returns a clone of the given array, with the given element added to the end. Important: It should be a NEW array instance, not the original array instance. var input = [1, 2]; var output = addToBackOfNew(input, 3); console.log(input); // --> [1, 2] console.log(output); // --> [1, 2, 3] function addToBackOfNew(arr, element) { }

function addToBackOfNew(arr, element) { var copy = arr.slice(); copy.push(element); return copy; }

Write a function called "addToFront". Given an array and an element, "addToFront" adds the given element to the front of the given array, and returns the given array. Notes: It should be the SAME array, not a new array. var output = addToFront([1, 2], 3); console.log(output); // -> [3, 1, 2] function addToFront(arr, element) { }

function addToFront(arr, element) { arr.unshift(element); return arr; }

Write a function called "isOldEnoughToDrink". Given a number, in this case an age, "isOldEnoughToDrink" returns whether a person of this given age is old enough to legally drink in the United States. Notes: The legal drinking age in the United States is 21. var output = isOldEnoughToDrink(22); console.log(output); // --> true function isOldEnoughToDrink(age) { }

function isOldEnoughToDrink(age) { if (age >= 21) { return true; } else { return false; } }

Write a function called "addToFrontOfNew". Given an array and an element, "addToFrontOfNew" returns a new array containing all the elements of the given array, with the given element added to the front. Important: It should be a NEW array instance, not the original array instance. var input = [1, 2]; var output = addToFrontOfNew(input, 3); console.log(output); // --> [3, 1, 2]; console.log(input); // --> [1, 2] function addToFrontOfNew(arr, element) { }

function addToFrontOfNew(arr, element) { var copy = arr.slice(); copy.unshift(element); return copy; }

Write a function called "areBothOdd". Given 2 numbers, "areBothOdd" returns whether or not both of the given numbers are odd. var output = areBothOdd(1, 3); console.log(output); // --> true function areBothOdd(num1, num2) { }

function areBothOdd(num1, num2) { if (num1 % 2 === 1 && num2 % 2 === 1) { return true; } else { return false; } }

Write a function called "areValidCredentials". Given a name and a password, "areValidCredentials", returns true if the name is longer than 3 characters, AND, the password is at least 8 characters long. Otherwise it returns false. var output = areValidCredentials('Ritu', 'mylongpassword') console.log(output); // --> true function areValidCredentials(name, password) { }

function areValidCredentials(name, password) { if (name.length > 3 && password.length >= 8) { return true; } else { return false; } }

Write a function called "average". Given two numbers, "average" returns their average. var output = average(4, 6); console.log(output); // --> 5 function average(num1, num2) { }

function average(num1, num2) { return (num1 + num2) / 2; }

Write a function called "isOldEnoughToDrive". Given a number, in this case an age, "isOldEnoughToDrive" returns whether a person of this given age is old enough to legally drive in the United States. Notes: The legal driving age in the United States is 16. var output = isOldEnoughToDrive(22); console.log(output); // --> true function isOldEnoughToDrive(age) { }

function isOldEnoughToDrive(age) { if (age >= 16) { return true; } else { return false; } }

Write a function called "isOldEnoughToVote". Given a number, in this case an age, 'isOldEnoughToVote' returns whether a person of this given age is old enough to legally vote in the United States. Notes: The legal voting age in the United States is 18. var output = isOldEnoughToVote(22); console.log(output); // --> true function isOldEnoughToVote(age) { }

function isOldEnoughToVote(age) { if (age >= 18) { return true; } else { return false; } }

Write a function called "calculateBillTotal". Given the pre tax and pre tip amount of a meal, "calculateBillTotal" returns the total amount due after tax and tip. Notes: Assume that sales tax is 9.5% and tip is 15%. Do NOT tip on the sales tax, only on the pre tip amount. var output = calculateBillTotal(20); console.log(output); // --> 24.9 function calculateBillTotal(preTaxAndTipAmount) { }

function calculateBillTotal(preTaxAndTipAmount) { var total = preTaxAndTipAmount + (preTaxAndTipAmount * 0.095) + (preTaxAndTipAmount * 0.15); return total; }

Write a function called "isSameLength". Given two words, "isSameLength" returns whether the given words have the same length. var output = isSameLength('words', 'super'); console.log(output); // --> true function isSameLength(word1, word2) { }

function isSameLength(word1, word2) { if (word1.length === word2.length){ return true; } else { return false; } }

Write a function called "checkAge". Given a person's name and age, "checkAge" returns one of two messages: "Go home, {insert_name_here}!", if they are younger than 21. "Welcome, {insert_name_here}!", if they are 21 or older. Naturally, replace "{insert_name_here}" with the given name. :) var output = checkAge('Adrian', 22); console.log(output); // --> 'Welcome, Adrian!' function checkAge(name, age) { }

function checkAge(name, age) { if(age >= 21) { return 'Welcome, ' + name + '!'; } else { return 'Go home, ' + name + '!'; } }

Write a function called "computeAreaOfACircle". Given the radius of a circle, "computeAreaOfACircle" returns its area. Notes: Math.PI can be used for pi. var output = computeAreaOfACircle(4); console.log(output); // --> 50.26548245743669 A = pi r^2

function computeAreaOfACircle(radius) { var area = Math.PI * (radius ** 2); return area; }

Write a function called "computeAreaOfARectangle". Given the length and width of a rectangle, "computeAreaOfARectangle" returns its area. var output = computeAreaOfARectangle(4, 8); console.log(output); // --> 32 function computeAreaOfARectangle(length, width) { }

function computeAreaOfARectangle(length, width) { return length * width; }

Write a function called "computeAreaOfATriangle". Given the base and height of a triangle, "computeAreaOfATriangle" returns its area. var output = computeAreaOfATriangle(4, 6); console.log(output); // --> 12 function computeAreaOfATriangle(base, height) { }

function computeAreaOfATriangle(base, height) { return (base * height) / 2; }

Write a function called "computeAverageLengthOfWords". Given two words, "computeAverageLengthOfWords" returns the average of their lengths. var output = computeAverageLengthOfWords('code', 'programs'); console.log(output); // --> 6 function computeAverageLengthOfWords(word1, word2) { }

function computeAverageLengthOfWords(word1, word2) { return (word1.length + word2.length) /2 ; }

Write a function called "computeAverageOfNumbers". Given an array of numbers, "computeAverageOfNumbers" returns their average. Notes: - If given an empty array, it should return 0. var input = [1,2,3,4,5]; var output = computeAverageOfNumbers(input); console.log(output); // --> 3 function computeAverageOfNumbers(nums) { }

function computeAverageOfNumbers(nums) { if (nums.length === 0) { return 0; } var result = 0; for (var i = 0; i < nums.length; i++) { result += nums[i]; } return result/nums.length; }

Write a function called "computeCompoundInterest". Given a principal, an interest rate, a compounding frequency, and a time (in years), "computeCompoundInterest" returns the amount of compound interest generated. var output = computeCompoundInterest(1500, .043, 4, 6); console.log(output); // --> 438.8368221341061 function computeCompoundInterest(principal, interestRate, compoundingFrequency, timeInYears) { }

function computeCompoundInterest(principal, interestRate, compoundingFrequency, timeInYears) { var compoundedInterest = (principal* Math.pow((1 +(interestRate/compoundingFrequency)), (compoundingFrequency * timeInYears))) - principal; return compoundedInterest; }

Write a function called "removeOddValues". Given an object, "removeOddValues" removes any properties whose values are odd numbers. var obj = { a: 2, b: 3, c: 4 }; removeOddValues(obj); console.log(obj); // --> { a: 2, c: 4 } function removeOddValues(obj) { }

function removeOddValues(obj) { for (var key in obj) { if (obj[key] % 2 === 1) { delete obj[key]; } } }

Write a function called "computeFactorialOfN". Given a natural number (a whole number greater than 0), "computeFactorialOfN" returns its factorial. var output = computeFactorialOfN(3); console.log(output); // --> 6 var output = computeFactorialOfN(4); console.log(output); // --> 24 //n * n-1 * n-2 * n- 3... function computeFactorialOfN(n) { }

function computeFactorialOfN(n) { var factorial = 1; for (var i = 1; i <= n; i++) { factorial *= i; } return factorial; }

Write a function called "computePerimeterOfACircle". Given the radius of a circle, "computePerimeterOfACircle" returns its perimeter. Notes: Math.PI can be used for pi. var output = computePerimeterOfACircle(4); console.log(output); // --> 25.132741228718345 C = 2πr function computePerimeterOfACircle(radius) { }

function computePerimeterOfACircle(radius) { return 2 * Math.PI * radius; }

Write a function called "computePerimeterOfARectangle". Given a length and a width describing a rectangle, "computePerimeterOfARectangle" returns its perimter. var output = computePerimeterOfARectangle(5, 2); console.log(output); // --> 14

function computePerimeterOfARectangle(length, width) { return (length * 2) + (width * 2); }

Write a function called "computePerimeterOfATriangle". Given 3 sides describing a triangle, "computePerimeterOfATriangle" returns its perimeter. var output = computePerimeterOfATriangle(6, 7, 10); console.log(output); // --> 23 function computePerimeterOfATriangle(side1, side2, side3) { }

function computePerimeterOfATriangle(side1, side2, side3) { return side1 + side2 + side3; }

Write a function called "computePower". Given a number and an exponent, "computePower" returns the given number, raised to the given exponent. var output = computePower(2, 3); console.log(output); // --> 8 function computePower(num, exponent) { }

function computePower(num, exponent) { return num ** exponent; }

Write a function called "removeProperty". Given an object and a key, "removeProperty" removes the given key from the given object. var obj = { name: 'Sam', age: 20 } removeProperty(obj, 'name'); console.log(obj.name); // --> undefined function removeProperty(obj, key) { }

function removeProperty(obj, key) { delete obj[key]; }

Write a function called "removeStringValues". Given an object, "removeStringValues" removes any properties on the given object whose values are strings. var obj = { name: 'Sam', age: 20 } removeStringValues(obj); console.log(obj); // { age: 20 } function removeStringValues(obj) { }

function removeStringValues(obj) { for (var key in obj){ if (typeof obj[key] === 'string') { delete obj[key]; } } }

Write a function called "computeProductOfAllElements". Given an array of numbers, "computeProductOfAllElements" returns the products of all the elements in the given array. Notes: If given array is empty, it should return 0. var output = computeProductOfAllElements([2, 5, 6]); console.log(output); // --> 60 function computeProductOfAllElements(arr) { }

function computeProductOfAllElements(arr) { if (arr.length === 0) { return 0; } var product = 1; for (var i = 0; i < arr.length; i++) { product *= arr[i] } return product; } (var product = 1; is 1 because anything multiplied by 1 is the same value!)

Write a function called "computeSquareRoot". Given a number, "computeSquareRoot" returns its square root. var output = computeSquareRoot(9); console.log(output); // --> 3 function computeSquareRoot(num) { }

function computeSquareRoot(num) { return Math. sqrt(num); }

Write a function called "computeSumBetween". Given 2 integers, "computeSumBetween" returns the sum between the two given integers, beginning at num1, and excluding num2. Notes: - The sum between 1 and 4 is 1 + 2 + 3 = 6. - If num2 is not greater than num1, it should return 0. var output = computeSumBetween(2, 5); console.log(output); // --> 9 function computeSumBetween(num1, num2) { }

function computeSumBetween(num1, num2) { if (num2 <= num1) { return 0; } var sum = 0; for (var i = num1; i < num2; i++){ sum = sum + i; } return sum; }

Write a function called "computeSumOfAllElements". Given an array of numbers, "computeSumOfAllElements" returns the sum of all the elements in the given array. var output = computeSumOfAllElements([1, 2, 3]) console.log(output); // --> 6 function computeSumOfAllElements(arr) { }

function computeSumOfAllElements(arr) { var sum = 0; for (var i = 0; i < arr.length; i++) { sum += arr[i]; } return sum; }

Write a function called "computeSummationToN". Given a number, "computeSummationToN" returns the sum of sequential numbers leading up to the given number, beginning at 0. Notes: - If n = 4, it should calculate the sum of 1 + 2 + 3 + 4, and return 10. var output = computeSummationToN(6); console.log(output); // --> 21 function computeSummationToN(n) { }

function computeSummationToN(n) { var sum = 0; for (var value = 0; value <= n; value++) { sum += value; } return sum; }

Write a function called "computeTripledAreaOfARectangle". Given a length and width of a rectangle, "computeTripledAreaOfARectangle" returns the rectangle's area, multiplied by 3. var output = computeTripledAreaOfARectangle(2, 4); console.log(output); // --> 24 function computeTripledAreaOfARectangle(length, width) { }

function computeTripledAreaOfARectangle(length, width) { var area = (length * width); return area * 3; }

Write a function called "convertDoubleSpaceToSingle". Given a string, "convertDoubleSpaceToSingle" returns the passed in string, with all the double spaces converted to single spaces. var output = convertDoubleSpaceToSingle("string with double spaces"); console.log(output); // --> "string with double spaces" function convertDoubleSpaceToSingle(str) { }

function convertDoubleSpaceToSingle(str) { var split = str.split(' '); var join = split.join(' '); return join; }

Write a function called "findShortestOfThreeWords". Given 3 strings, "findShortestOfThreeWords" returns the shortest of the given strings. Notes: If there are ties, it should return the first word in the parameters list. var output = findShortestOfThreeWords('a', 'two', 'three'); console.log(output); // --> 'a' function findShortestOfThreeWords(word1, word2, word3) { }

function findShortestOfThreeWords(word1, word2, word3) { if (word1.length <= word2.length) { return word1; } else if (word2.length <= word3.length) { return word2; } else { return word3; } }

Write a function called "findSmallestElement". Given an array of numbers, "findSmallestElement" returns the smallest number within the given array. Notes: If the given array is empty, it should return 0. var output = findSmallestElement([4, 1, 9, 10]); console.log(output); // --> 1 function findSmallestElement(arr) { }

function findSmallestElement(arr) { if (arr.length === 0) { return 0; } var smallest = arr[0]; for (var i = 0; i < arr.length; i++){ if (arr[i] < smallest) { smallest = arr[i]; } } return smallest; }

Write a function called "convertScoreToGrade". Given a score, "convertScoreToGrade" returns a string representing the letter grade corresponding to the given score. Notes: (100 - 90) --> 'A' (89 - 80) --> 'B' (79 - 70) --> 'C' (69 - 60) --> 'D' (59 - 0) --> 'F' If the given score is greater than 100 or less than 0, it should return 'INVALID SCORE'. var output = convertScoreToGrade(91); console.log(output); // --> 'A' function convertScoreToGrade(score) { }

function convertScoreToGrade(score) { if (score >= 100 || score < 0) { return 'INVALID SCORE'; } if (score <= 59) { return 'F'; } else if (score <= 69) { return 'D'; } else if (score <= 79) { return 'C'; } else if (score <= 89) { return 'B'; } else if (score <= 100) { return 'A'; } }

Write a function called "addArrayProperty". Given an object, a key, and an array, "addArrayProperty" sets a new property on the object at the given key, with its value set to the given array. var myObj = {}; var myArray = [1, 3]; addArrayProperty(myObj, 'myProperty', myArray); console.log(myObj.myProperty); // --> [1, 3] function addArrayProperty(obj, key, arr) { }

function addArrayProperty(obj, key, arr) { obj[key] = arr; }

Write a function called "addFullNameProperty". Given an object that has a "firstName" property and a "lastName" property, "addFullNameProperty" sets a "fullName" property on the input object, whose value is a string with the first name and last name separated by a space. var person = { firstName: 'Jade', lastName: 'Smith' }; addFullNameProperty(person); console.log(person.fullName); // --> 'Jade Smith' function addFullNameProperty(obj) { }

function addFullNameProperty(obj) { var firstName = obj.firstName; var lastName = obj.lastName; var fullNameValue = firstName + ' ' + lastName; obj['fullName'] = fullNameValue; }

Write a function called "convertScoreToGradeWithPlusAndMinus". Given a score, "convertScoreToGradeWithPlusAndMinus" returns a string representing the letter grade corresponding to the given score. Notes: (100 - 90) --> 'A' (89 - 80) --> 'B' (79 - 70) --> 'C' (69 - 60) --> 'D' (59 - 0) --> 'F' - If the given score is greater than 100 or less than 0, it should return 'INVALID SCORE'. - If the score is between the 0 and 2 (inclusive) of a given range, return the letter with a '-' - If the score is between the 8 and 9 (inclusive) of a given range, return the letter with a '+' - There are is no F+ and there is no F-. var output = convertScoreToGradeWithPlusAndMinus(91); console.log(output); // --> 'A-' function convertScoreToGradeWithPlusAndMinus(score) { }

function convertScoreToGradeWithPlusAndMinus(score) { if (score > 100 || score < 0) { return 'INVALID SCORE'; } if (score <= 59) { return 'F'; } else if (score <= 69) { if(score <= 62) { return 'D-'; } else if (score >= 68) { return 'D+'; } else { return 'D'; } } else if (score <= 79) { if(score <= 72) { return 'C-'; } else if (score >= 78) { return 'C+'; } else { return 'C'; } } else if (score <= 89) { if(score <= 82) { return 'B-'; } else if (score >= 88) { return 'B+'; } else { return 'B'; } } else if (score <= 100) { if(score <= 92) { return 'A-'; } else if (score >= 98) { return 'A+'; } else { return 'A'; } } }

Write a function called "countAllCharacters". Given a string, "countAllCharacters" returns an object where each key is a character in the given string. The value of each key should be how many times each character appeared in the given string. Notes: If given an empty string, countAllCharacters should return an empty object. var output = countAllCharacters('banana'); console.log(output); // --> {b: 1, a: 3, n: 2} function countAllCharacters(str) { }

function countAllCharacters(str) { var result = {}; for (var i = 0; i < str.length; i++) { if (result[str[i]]=== undefined) { result[str[i]] = 1; } else { result[str[i]] += 1; } } return result; }

Write a function called "countCharacter". Given a string input and a character, "countCharacter" returns the number of occurrences of a given character in the given string. var output = countCharacter('I am a hacker', 'a'); console.log(output); // --> 3 function countCharacter(str, char) { }

function countCharacter(str, char) { var count = 0; for (var i = 0; i < str.length; i++) { if (str[i] === char) { count++; } } return count; }

Write a function called "countNumberOfKeys". Given an object, "countNumberOfKeys" returns how many properties the given object has. var obj = { a: 1, b: 2, c: 3 }; var output = countNumberOfKeys(obj); console.log(output); // --> 3 function countNumberOfKeys(obj) { }

function countNumberOfKeys(obj) { var count = 0; for (var key in obj){ count += 1; } return count; }

Write a function called "countWords". Given a string, "countWords" returns an object where each key is a word in the given string, with its value being how many times that word appeared in the given string. Notes: If given an empty string, it should return an empty object. var output = countWords('ask a bunch get a bunch'); console.log(output); // --> {ask: 1, a: 2, bunch: 2, get: 1} function countWords(str) { }

function countWords(str) { if (str === ''){ return {}; } var result = {}; var splitString = str.split(' '); for (var i = 0; i < splitString.length; i++){ if (result[splitString[i]] === undefined) { result[splitString[i]] = 1; } else { result[splitString[i]] += 1; } } return result; }

Write a function called "cube". Given a number, "cube" returns the cube of that number. var output = cube(3); console.log(output); // --> 27 function cube(num) { }

function cube(num) { return num ** 3; }

Write a function called "doubleSquareRootOf". Given a number, "doubleSquareRootOf" returns double its square root. var output = doubleSquareRootOf(121); console.log(output); // --> 22 function doubleSquareRootOf(num) { }

function doubleSquareRootOf(num) { return Math. sqrt(num) * 2; }

Write a function called "equalsTen". Given a number, "equalsTen" returns whether or not the given number is 10. var output = equalsTen(9); console.log(output); // --> false function equalsTen(num) { }

function equalsTen(num) { if (num === 10) { return true; } else { return false; } }

Write a function called "extend". Given two objects, "extend" adds properties from the 2nd object to the 1st object. Notes: Add any keys that are not in the 1st object. If the 1st object already has a given key, ignore it (do not overwrite the property value). Do not modify the 2nd object at all. var obj1 = { a: 1, b: 2 }; var obj2 = { b: 4, c: 3 }; extend(obj1, obj2); console.log(obj1); // --> {a: 1, b: 2, c: 3} console.log(obj2); // --> {b: 4, c: 3} function extend(obj1, obj2) { }

function extend(obj1, obj2) { for (var keyFromObj2 in obj2) { if (obj1[keyFromObj2] === undefined) { obj1[keyFromObj2] = obj2[keyFromObj2]; } } }

Write a function called "filterEvenLengthWords". Given an array of strings, "filterEvenLengthWords" returns an array containing only the elements of the given array whose length is an even number. var output = filterEvenLengthWords(['word', 'words', 'word', 'words']); console.log(output); // --> ['word', 'word'] function filterEvenLengthWords(words) { }

function filterEvenLengthWords(words) { var evenLengthWords = []; for (var i = 0; i < words.length; i++ ){ if (words[i].length % 2 === 0){ evenLengthWords.push(words[i]); } } return evenLengthWords; }

Write a function called "filterOddElements". Given an array of numbers, "filterOddElements" returns an array containing only the odd numbers of the given array. var output = filterOddElements([1, 2, 3, 4, 5]); console.log(output); // --> [1, 3, 5] function filterOddElements(arr) { }

function filterOddElements(arr) { var oddElements = []; for (var i = 0; i < arr.length; i++) { if (arr[i] % 2 === 1) { oddElements.push(arr[i]); } } return oddElements; }

Write a function called "filterOddLengthWords". Given an array of strings, "filterOddLengthWords" returns an array containing only the elements of the given array whose lengths are odd numbers. var output = filterOddLengthWords(['there', 'it', 'is', 'now']); console.log(output); // --> ['there', "now'] function filterOddLengthWords(words) { }

function filterOddLengthWords(words) { var oddLengthWords = []; for (var i = 0; i < words.length; i++){ if (words[i].length % 2 === 1) { oddLengthWords.push(words[i]); } } return oddLengthWords; }

Write a function called "findSmallestNumberAmongMixedElements". Given an array of mixed elements, "findSmallestNumberAmongMixedElements" returns the smallest number within the given array. Notes: - If the given array is empty, it should return 0. - If the array contains no numbers, it should return 0. var output = findSmallestNumberAmongMixedElements([4, 'lincoln', 9, 'octopus']); console.log(output); // --> 4 function findSmallestNumberAmongMixedElements(arr) { }

function findSmallestNumberAmongMixedElements(arr) { if (arr.length === 0){ return 0; } var arrayOfNumbers = []; for (var i = 0; i < arr.length; i++) { if (typeof arr[i] === 'number') { arrayOfNumbers.push(arr[i]); } } if (arrayOfNumbers.length === 0) { return 0; } var smallestNumber = arrayOfNumbers[0]; for (var j = 0; j < arrayOfNumbers.length; j++) { if (smallestNumber > arrayOfNumbers[j]) { smallestNumber = arrayOfNumbers[j]; } } return smallestNumber; }

Write a function called "getAllButLastElementOfProperty". Given an object and a key, "getAllButLastElementOfProperty" returns an array containing all but the last element of the array located at the given key. Notes: - If the array is empty, it should return an empty array. - If the property at the given key is not an array, it return an empty array. - If there is no property at the key, it should return an empty array. var obj = { key: [1, 2, 3] }; var output = getAllButLastElementOfProperty(obj, 'key'); console.log(output); // --> [1,2] function getAllButLastElementOfProperty(obj, key) { }

function getAllButLastElementOfProperty(obj, key) { if (obj[key] === undefined) { return []; } if (Array.isArray(obj[key]) === false){ return []; } if (obj[key].length === 0){ return []; } var copyOfArray = obj[key].slice(); for (var i = 0; i < obj[key].length; i++) { var lastIndex = obj[key].length - 1; copyOfArray.splice(lastIndex, 1); } return copyOfArray; }

Write a function called "getAllElementsButFirst". Given an array, "getAllElementsButFirst" returns an array with all the elements but the first. var input = [1, 2, 3, 4]; var output = getAllElementsButFirst(input); console.log(output); // --> [2, 3, 4] function getAllElementsButFirst(array) { }

function getAllElementsButFirst(array) { return array.slice(1) }

Write a function called "getAllElementsButLast". Given an array, "getAllElementsButLast" returns an array with all the elements but the last. var input = [1, 2, 3, 4]; var output = getAllElementsButLast(input); console.log(output); // --> [1, 2 , 3] function getAllElementsButLast(array) { }

function getAllElementsButLast(array) { var lastIndex = array.length - 1; return array.slice(0, lastIndex); }

Write a function called "getAllElementsButNth". Given an array and an index, "getAllElementsButNth" returns an array with all the elements but the nth. var output = getAllElementsButNth(['a', 'b', 'c'], 1); console.log(output); // --> ['a', 'c'] function getAllElementsButNth(array, n) { }

function getAllElementsButNth(array, n) { array.splice(n, 1); return array; }

Write a function called "getAllLetters". Given a word, "getAllLetters" returns an array containing every character in the word. Notes: If given an empty string, it should return an empty array. var output = getAllLetters('Radagast'); console.log(output); // --> ['R', 'a', 'd', 'a', 'g', 'a', 's', 't'] function getAllLetters(str) { }

function getAllLetters(str) { if (str === '') { return []; } var splitted = str.split(''); return splitted; }

Write a function called "getAllWords". Given a sentence, "getAllWords" returns an array containing every word in the sentence. Notes: If given an empty string, it should return an empty array. var output = getAllWords('Radagast the Brown'); console.log(output); // --> ['Radagast', 'the', 'Brown'] function getAllWords(str) { }

function getAllWords(str) { if (str === '') { return []; } var splitted = str.split(' '); return splitted; } (note: in order to split the string properly, the .split() method is taking in a space (seen as ' ') as the indicator of where the string should split)

Write a function called "getAverageOfElementsAtProperty". Given an object and a key, "getAverageOfElementsAtProperty" returns the average of all the elements in the array located at the given key. Notes: - If the array at the given key is empty, it should return 0. - If the property at the given key is not an array, it should return 0. - `If there is no property at the given key, it should return 0. var obj = { key: [1, 2, 3] }; var output = getAverageOfElementsAtProperty(obj, 'key'); console.log(output); // --> 2 function getAverageOfElementsAtProperty(obj, key) { }

function getAverageOfElementsAtProperty(obj, key) { if(obj[key] === undefined) { return 0; } if(Array.isArray(obj[key]) === false) { return 0; } if(obj[key].length === 0) { return 0; } var resultProduct = 0; for (var i = 0; i< obj[key].length; i++){ resultProduct += obj[key][i]; } return resultProduct/obj[key].length; }

Write a function called "getElementOfArrayProperty". Given an object, a key, and a numerical index, "getElementOfArrayProperty" returns the value of the element at the given index of the array located within the given object at the given key. Notes: - If the array is empty, it should return undefined. - If the given index is out of range of the array located at the given key, it should return undefined. - If the property at the given key is not an array, it should return undefined. - If there is no property at the key, it should return undefined. var obj = { key: ['Jamil', 'Albrey'] }; var output = getElementOfArrayProperty(obj, 'key', 0); console.log(output); // --> 'Jamil' function getElementOfArrayProperty(obj, key, index) { }

function getElementOfArrayProperty(obj, key, index) { if(obj[key] === undefined) { return undefined; } if(Array.isArray(obj[key]) === false) { return undefined; } if(obj[key].length === 0) { return undefined; } return obj[key][index]; }

Write a function called "getElementsAfter". Given an array and an index, "getElementsAfter" returns a new array with all the elements after (but not including) the given index. Notes: In order to do this you should be familiar with the 'slice' method. var output = getElementsAfter(['a', 'b', 'c', 'd', 'e'], 2); console.log(output); // --> ['d', 'e'] function getElementsAfter(array, n) { }

function getElementsAfter(array, n) { return array.slice(n + 1); }

Write a function called "getLargestElement". Given an array, "getLargestElement" returns the largest number in the given array. Notes: It should return 0 if the array is empty. var output = getLargestElement([5, 2, 8, 3]); console.log(output); // --> 8; function getLargestElement(arr) { }

function getLargestElement(arr) { if (arr.length === 0) { return 0; } var largestElement = arr[0]; for (var i = 0; i < arr.length; i++){ if (arr[i] > largestElement) { largestElement = arr[i]; } } return largestElement; }

Write a function called "getElementsGreaterThan10AtProperty". Given an object and a key, "getElementsGreaterThan10AtProperty" returns an array containing the elements within the array, located at the given key, that are greater than 10. Notes: - If the array is empty, it should return an empty array. - If the array contains no elements greater than 10, it should return an empty array. - If the property at the given key is not an array, it should return an empty array. - If there is no property at the key, it should return an empty array. var obj = { key: [1, 20, 30] }; var output = getElementsGreaterThan10AtProperty(obj, 'key'); console.log(output); // --> [20, 30] function getElementsGreaterThan10AtProperty(obj, key) { }

function getElementsGreaterThan10AtProperty(obj, key) { if(obj[key] === undefined) { return []; } if (Array.isArray(obj[key]) === false) { return []; } if(obj[key].length === 0) { return []; } var result = []; for (var i = 0; i < obj[key].length; i ++){ if (obj[key][i] > 10) { result.push(obj[key][i]); } } return result; }

Write a function called "getElementsLessThan100AtProperty". Given an object and a key, "getElementsLessThan100AtProperty" returns an array containing all the elements of the array located at the given key that are less than 100. Notes: - If the array is empty, it should return an empty array. If the array contains no elements less than 100, it should return an empty array. - If the property at the given key is not an array, it should return an empty array. - If there is no property at the key, it should return an empty array. var obj = { key: [1000, 20, 50, 500] }; var output = getElementsLessThan100AtProperty(obj, 'key'); console.log(output); // --> [20, 50] function getElementsLessThan100AtProperty(obj, key) { }

function getElementsLessThan100AtProperty(obj, key) { if (obj[key] === undefined) { return []; } if(Array.isArray(obj[key]) === false) { return []; } if (obj[key].length === 0){ return []; } var elementsThatAreLessThan100 = []; for (var i = 0; i < obj[key].length; i++){ if (obj[key][i] < 100) { elementsThatAreLessThan100.push(obj[key][i]); } } return elementsThatAreLessThan100; }

Write a function called "getElementsUpTo". Given an array and a index, "getElementsUpTo", returns an array with all the elements up until, but not including, the element at the given index. Notes: In order to do this you should be familiar with the 'slice' method. var output = getElementsUpTo(['a', 'b', 'c', 'd', 'e'], 3) console.log(output); // --> ['a', 'b', 'c'] function getElementsUpTo(array, n) { }

function getElementsUpTo(array, n) { return array.slice(0, n); }

Write a function called "getEvenElementsAtProperty". Given an object and a key, "getEvenElementsAtProperty" returns an array containing all the even elements of the array located at the given key. Notes: - If the array is empty, it should return an empty array. - If the array contains no even elements, it should return an empty array. - If the property at the given key is not an array, it should return an empty array. - If there is no property at the given key, it should return an empty array. var obj = { key: [1000, 11, 50, 17] }; var output = getEvenElementsAtProperty(obj, 'key'); console.log(output); // --> [1000, 50] function getEvenElementsAtProperty(obj, key) { }

function getEvenElementsAtProperty(obj, key) { if (obj[key] === undefined) { return []; } if(Array.isArray(obj[key]) === false) { return []; } if(obj[key].length === 0) { return []; } var resultArray = []; for (var i = 0; i < obj[key].length; i++) { if (obj[key][i] % 2 === 0){ resultArray.push(obj[key][i]); } } return resultArray; }

Write a function called "getEvenLengthWordsAtProperty". Given an object and a key, "getEvenLengthWordsAtProperty" returns an array containing all the even length word elements of the array located at the given key. Notes: - If the array is empty, it should return an empty array. - If it contains no even length elements, it should return an empty array. - If the property at the given key is not an array, it should return an empty array. - If there is no property at the key, it should return an empty array. var obj = { key: ['a', 'long', 'game'] }; var output = getEvenLengthWordsAtProperty(obj, 'key'); console.log(output); // --> ['long', 'game'] function getEvenLengthWordsAtProperty(obj, key) { }

function getEvenLengthWordsAtProperty(obj, key) { if(obj[key] === undefined) { return []; } if (Array.isArray(obj[key]) === false){ return []; } if (obj[key].length === 0){ return []; } var resultArray = []; for (var i = 0; i < obj[key].length; i++){ if (obj[key][i].length % 2 === 0) { resultArray.push(obj[key][i]); } } return resultArray; }

Write a function called "getFirstElement". Given an array, "getFirstElement" returns the first element of the given array. Notes: If the given array has a length of 0, it should return undefined. var output = getFirstElement([1, 2, 3, 4, 5]); console.log(output); // --> 1 function getFirstElement(array) { }

function getFirstElement(array) { return array[0]; }

Write a function called "getFirstElementOfProperty". Given an object and a key, "getFirstElementOfProperty" returns the first element of the array located at the given key. Notes: - If the array is empty, it should return undefined. - If the property at the given key is not an array, it should return undefined. - If there is no property at the key, it should return undefined. var obj = { key: [1, 2, 4] }; var output = getFirstElementOfProperty(obj, 'key'); console.log(output); // --> 1 function getFirstElementOfProperty(obj, key) { }

function getFirstElementOfProperty(obj, key) { if (obj[key] === undefined){ return undefined; } if (Array.isArray(obj[key]) === false) { return undefined; } if (obj[key].length === 0){ return undefined; } return obj[key][0]; }

Write a function called "getFullName". Given a first and a last name, "getFullName" returns a single string with the given first and last names separated by a single space. var output = getFullName('Joe', 'Smith'); console.log(output); // --> 'Joe Smith' function getFullName (firstName, lastName) { }

function getFullName (firstName, lastName) { return firstName + ' ' + lastName; }

Write a function called "getIndexOf". Given a character and a string, "getIndexOf" returns the first position of the given character in the given string. Notes: - Strings are zero indexed, meaning the first character in a string is at position 0. - When a string contains more than one occurrence of a character, it should return the index of its first occurrence. - If the character does not exist in the string, it should return -1. - Do not use the native indexOf function in your implementation. var output = getIndexOf('a', 'I am a hacker'); console.log(output); // --> 2 function getIndexOf(char, str) { }

function getIndexOf(char, str) { for (var i = 0; i < str.length; i++){ if(str[i] === char ) { return i; } } return -1; }

Write a function called "getLargestElementAtProperty". Given an object and a key, "getLargestElementAtProperty" returns the largest element in the array located at the given key. Notes: - If the array is empty, it should return undefined. - If the property at the given key is not an array, it should return undefined. - If there is no property at the key, it should return undefined. var obj = { key: [1, 2, 4] }; var output = getLargestElementAtProperty(obj, 'key'); console.log(output); // --> 4 function getLargestElementAtProperty(obj, key) { }

function getLargestElementAtProperty(obj, key) { if(obj[key] === undefined){ return undefined; } if(Array.isArray(obj[key]) === false){ return undefined; } if(obj[key].length === 0) { return undefined; } var largestElement = obj[key][0]; for (var i = 0; i < obj[key].length; i++) { if (obj[key][i] > largestElement) { largestElement = obj[key][i]; } } return largestElement; }

Write a function called "getLargestNumberAmongMixedElements". Given any array, "getLargestNumberAmongMixedElements" returns the largest number in the given array. Notes: - The array might contain values of a type other than numbers. - If the array is empty, it should return 0. - If the array contains no numbers, it should return 0. var output = getLargestNumberAmongMixedElements([3, 'word', 5, 'up', 3, 1]); console.log(output); // --> 5 function getLargestNumberAmongMixedElements(arr) { }

function getLargestNumberAmongMixedElements(arr) { if (arr.length === 0){ return 0; } var arrayOfNumbers = []; for (var i = 0; i < arr.length; i++){ if (typeof arr[i] === "number"){ arrayOfNumbers.push(arr[i]); } } if (arrayOfNumbers.length === 0) { return 0; } var largestNumber = arrayOfNumbers[0]; for (var j = 0; j < arrayOfNumbers.length; j++) { if (largestNumber < arrayOfNumbers[j]) { largestNumber = arrayOfNumbers[j]; } } return largestNumber; }

Write a function called "getLastElement". Given an array, "getLastElement" returns the last element of the given array. Notes: If the given array has a length of 0, it should return 'undefined'. var output = getLastElement([1, 2, 3, 4]); console.log(output); // --> 4 function getLastElement(array) { }

function getLastElement(array) { var lastElement = array.length - 1; return array[lastElement]; }

Write a function called "getLastElementOfProperty". Given an object and a key, "getLastElementOfProperty" returns the last element of an array located at the given key. Notes: - If the array is empty, it should return undefined. - If the property at the given key is not an array, it should return undefined. - If there is no property at the key, it should return undefined. var obj = { key: [1, 2, 5] }; var output = getLastElementOfProperty(obj, 'key'); console.log(output); // --> 5 function getLastElementOfProperty(obj, key) { }

function getLastElementOfProperty(obj, key) { if (obj[key] === undefined) { return undefined; } if (Array.isArray(obj[key]) === false){ return undefined; } if (obj[key].length === 0) { return undefined; } var lastIndex = (obj[key].length) - 1; return obj[key][lastIndex]; }

Write a function called "getLengthOfLongestElement". Given an array, "getLengthOfLongestElement" returns the length of the longest string in the given array. Notes: It should return 0 if the array is empty. var output = getLengthOfLongestElement(['one', 'two', 'three']); console.log(output); // --> 5 function getLengthOfLongestElement(arr) { }

function getLengthOfLongestElement(arr) { if (arr.length === 0) { return 0; } var longestString = arr[0]; for (var i = 0; i < arr.length; i++) { if (arr[i].length > longestString.length) { longestString = arr[i]; } } return longestString.length }

Write a function called "getLengthOfShortestElement". Given an array, "getLengthOfShortestElement" returns the length of the shortest string in the given array. Notes: It should return 0 if the array is empty. var output = getLengthOfShortestElement(['one', 'two', 'three']); console.log(output); // --> 3 function getLengthOfShortestElement(arr) { }

function getLengthOfShortestElement(arr) { if (arr.length === 0) { return 0; } var shortest = arr[0]; for (var i = 0; i < arr.length; i++) { if (arr[i].length < shortest.length) { shortest = arr[i] ; } } return shortest.length; }

Write a function called "getLengthOfThreeWords". Given 3 words, "getLengthOfThreeWords" returns the sum of their lengths. var output = getLengthOfThreeWords('some', 'other', 'words'); console.log(output); // --> 14 function getLengthOfThreeWords(word1, word2, word3) { }

function getLengthOfThreeWords(word1, word2, word3) { return word1.length + word2.length + word3.length; }

Write a function called "getLengthOfTwoWords". Given 2 words, "getLengthOfTwoWords" returns the sum of their lengths. var output = getLengthOfTwoWords('some', 'words'); console.log(output); // --> 9 function getLengthOfTwoWords(word1, word2) { }

function getLengthOfTwoWords(word1, word2) { return word1.length + word2.length; }

Write a function called "getLengthOfWord". Given a word, "getLengthOfWord" returns the length of the given word. var output = getLengthOfWord('some'); console.log(output); // --> 4 function getLengthOfWord(word) { }

function getLengthOfWord(word) { return word.length; }

Write a function called "getLongestElement". Given an array, "getLongestElement" returns the longest string in the given array. Notes: - If there are ties, it returns the first element to appear. - If the array is empty, it should return an empty string. var output = getLongestElement(['one', 'two', 'three']); console.log(output); // --> 'three' function getLongestElement(arr) { }

function getLongestElement(arr) { if (arr.length === 0) { return ''; } var longest = arr[0]; for (var i = 0; i < arr.length; i++) { if (arr[i].length > longest.length) { longest = arr[i]; } } return longest; }

Write a function called "getLongestOfThreeWords". Given 3 words, "getLongestOfThreeWords" returns the longest of three words. Notes: If there is a tie, it should return the first word in the tie. var output = getLongestOfThreeWords('these', 'three', 'words'); console.log(output); // --> 'these' function getLongestOfThreeWords(word1, word2, word3) { }

function getLongestOfThreeWords(word1, word2, word3) { if (word1.length >= word2.length) { return word1; } else if (word2.length >= word3.length) { return word2; } else { return word3; } }

Write a function called "isOddLength". Given a word, "isOddLength" returns whether the length of the given word is odd. var output = isOddLength('special'); console.log(output); // --> true function isOddLength(word) { }

function isOddLength(word) { if (word.length % 2 === 1) { return true; } else { return false; } }

Write a function called "getLongestWordOfMixedElements". Given an array of mixed types, "getLongestWordOfMixedElements" returns the longest string in the given array. Notes: - If the array is empty, it should return an empty string (""). - If the array contains no strings; it should return an empty string. var output = getLongestWordOfMixedElements([3, 'word', 5, 'up', 3, 1]); console.log(output); // --> 'word' function getLongestWordOfMixedElements(arr) { }

function getLongestWordOfMixedElements(arr) { if (arr === undefined) { return ""; } var arrayOfStrings = []; for (var i = 0; i < arr.length; i++) { if (typeof arr[i] === 'string') { arrayOfStrings.push(arr[i]); } } if (arrayOfStrings.length === 0) { return ''; } var longestWord = arrayOfStrings[0]; for (var j = 0; j < arrayOfStrings.length; j++){ if (longestWord.length < arrayOfStrings[j].length) { longestWord = arrayOfStrings[j]; } } return longestWord; }

Write a function called "getNthElement". Given an array and an integer, "getNthElement" returns the element at the given integer, within the given array. Notes: If the array has a length of 0, it should return 'undefined'. var output = getNthElement([1, 3, 5], 1); console.log(output); // --> 3 function getNthElement(array, n) { }

function getNthElement(array, n) { return array[n]; }

Write a function called "getNthElementOfProperty". Given an object and a key, "getNthElementOfProperty" returns the nth element of an array located at the given key. Notes: - If the array is empty, it should return undefined. - If n is out of range, it should return undefined. - If the property at the given key is not an array, it should return undefined. - If there is no property at the key, it should return undefined. var obj = { key: [1, 2, 6] }; var output = getNthElementOfProperty(obj, 'key', 1); console.log(output); // --> 2 function getNthElementOfProperty(obj, key, n) { }

function getNthElementOfProperty(obj, key, n) { if (obj[key] === undefined) { return undefined; } if (Array.isArray(obj[key]) === false) { return undefined; } return obj[key][n]; }

Write a function called "getOddElementsAtProperty". Given an object and a key, "getOddElementsAtProperty" returns an array containing all the odd elements of the array located at the given key. Notes: - If the array is empty, it should return an empty array. - If it contains no odd elements, it should return an empty array. - If the property at the given key is not an array, it should return an empty array. - If there is no property at the key, it should return an empty array. var obj = { key: [1, 2, 3, 4, 5] }; var output = getOddElementsAtProperty(obj, 'key'); console.log(output); // --> [1, 3, 5] function getOddElementsAtProperty(obj, key) { }

function getOddElementsAtProperty(obj, key) { if (obj[key] === undefined){ return []; } if(Array.isArray(obj[key]) === false) { return []; } if(obj[key].length === 0) { return []; } var resultArray = []; for (var i = 0; i < obj[key].length; i++){ if (obj[key][i] % 2 === 1) { resultArray.push(obj[key][i]); } } return resultArray; }

Write a function called "getOddLengthWordsAtProperty". Given an object and a key, "getOddLengthWordsAtProperty" returns an array containing all the odd length word elements of the array located at the given key. Notes: - If the array is empty, it should return an empty array. - If it contains no odd length elements, it should return an empty array. - If the property at the given key is not an array, it should return an empty array. - If there is no property at the given key, it should return an empty array. var obj = { key: ['It', 'has', 'some', 'words'] }; var output = getOddLengthWordsAtProperty(obj, 'key'); console.log(output); // --> ['has', 'words'] function getOddLengthWordsAtProperty(obj, key) { }

function getOddLengthWordsAtProperty(obj, key) { if (obj[key] === undefined) { return []; } if(Array.isArray(obj[key]) === false) { return []; } if (obj[key].length === 0) { return []; } var resultArray = []; for (var i = 0; i < obj[key].length; i++){ if (obj[key][i].length % 2 === 1) { resultArray.push(obj[key][i]); } } return resultArray; }

Write a function called "getProductOfAllElementsAtProperty". Given an object and a key, "getProductOfAllElementsAtProperty" returns the product of all the elements in the array located at the given key. Notes: If the array is empty, it should return 0. If the property at the given key is not an array, it should return 0. If there is no property at the given key, it should return 0. var obj = { key: [1, 2, 3, 4] }; var output = getProductOfAllElementsAtProperty(obj, 'key'); console.log(output); // --> 24 function getProductOfAllElementsAtProperty(obj, key) { }

function getProductOfAllElementsAtProperty(obj, key) { if (obj[key] === undefined) { return 0; } if (Array.isArray(obj[key]) === false){ return 0; } if(obj[key].length === 0){ return 0; } var resultProduct = 1; for (var i = 0; i < obj[key].length; i++) { resultProduct *= obj[key][i]; } return resultProduct; }

Write a function called "getProperty". Given an object and a key, "getProperty" returns the value of the property at the given key. Notes: If there is no property at the given key, it should return undefined. var obj = { key: 'value' }; var output = getProperty(obj, 'key'); console.log(output); // --> 'value' function getProperty(obj, key) { }

function getProperty(obj, key) { return obj[key]; }

Write a function called "getSmallestElementAtProperty". Given an object and a key, "getSmallestElementAtProperty" returns the smallest element in the array located at the given key. Notes: - If the array is empty, it should return undefined. - If the property at the given key is not an array, it should return undefined. - If there is no property at the key, it should return undefined. var obj = { key: [2, 1, 5] }; var output = getSmallestElementAtProperty(obj, 'key'); console.log(output); // --> 1 function getSmallestElementAtProperty(obj, key) { }

function getSmallestElementAtProperty(obj, key) { if(obj[key] === undefined) { return undefined; } if(Array.isArray(obj[key]) === false) { return undefined; } if(obj[key].length === 0) { return undefined; } var smallestElement = obj[key][0]; for (var i = 0; i < obj[key].length; i++) { if (obj[key][i] < smallestElement) { smallestElement = obj[key][i]; } } return smallestElement; }

Write a function called "isOddWithoutModulo". Given a number, "isOddWithoutModulo" returns whether the passed in number is odd. Note: - It does so without using the modulo operator (%). - It should work for negative numbers and zero. var output = isOddWithoutModulo(17); console.log(output); // --> true function isOddWithoutModulo(num) { }

function isOddWithoutModulo(num) { if (num === 0) { return false; } num = Math.abs(num); while (num >= 2){ num = num - 2; } if (num === 1) { return true; } else { return false; } }

Write a function called "getSquaredElementsAtProperty". Given an object and a key, "getSquaredElementsAtProperty" returns an array containing all the squared elements of the array located at the given key. Notes: - If the array is empty, it should return an empty array. - If the property at the given key is not an array, it should return an empty array. - If there is no property at the key, it should return an empty array. var obj = { key: [2, 1, 5] }; var output = getSquaredElementsAtProperty(obj, 'key'); console.log(output); // --> [4, 1, 25] function getSquaredElementsAtProperty(obj, key) { }

function getSquaredElementsAtProperty(obj, key) { if(obj[key] === undefined) { return []; } if (Array.isArray(obj[key]) === false) { return []; } if(obj[key].length === 0) { return []; } var resultArray = []; for (var i = 0; i < obj[key].length; i++) { resultArray.push(obj[key][i] ** 2); } return resultArray; }

Write a function called "getSumOfAllElementsAtProperty". Given an object and a key, "getSumOfAllElementsAtProperty" returns the sum of all the elements in the array located at the given key. Notes: - If the array is empty, it should return 0. - If the property at the given key is not an array, it should return 0. - If there is no property at the key, it should return 0. var obj = { key: [4, 1, 8] }; var output = getSumOfAllElementsAtProperty(obj, 'key'); console.log(output); // --> 13 function getSumOfAllElementsAtProperty(obj, key) { function getSumOfAllElementsAtProperty(obj, key) { }

function getSumOfAllElementsAtProperty(obj, key) { if(obj[key] === undefined) { return 0; } if(Array.isArray(obj[key]) === false) { return 0; } if (obj[key].length === 0) { return 0; } var resultSum = 0; for (var i = 0; i < obj[key].length; i++) { resultSum += obj[key][i]; } return resultSum; }

Write a function called "isEitherEven". Given two numbers, "isEitherEven" returns whether or not either one of the given numbers is even. var output = isEitherEven(1, 4); console.log(output); // --> true function isEitherEven(num1, num2) { }

function isEitherEven(num1, num2) { if ((num1 % 2 === 0) || (num2 % 2 === 0)) { return true; } else { return false; } }

Write a function called "isEitherEvenAndLessThan9". Given two numbers, 'isEitherEvenAndLessThan9' returns whether at least one of them is even, and, both of them are less than 9. var output = isEitherEvenAndLessThan9(2, 4); console.log(output); // --> true function isEitherEvenAndLessThan9(num1, num2) { }

function isEitherEvenAndLessThan9(num1, num2) { if (((num1 % 2 === 0) || (num2 % 2 === 0)) && ((num1 < 9) && (num2 < 9))) { return true; } else { return false; } }

Write a function called "isEitherEvenOrAreBoth7". Given two numbers, 'isEitherEvenOrAreBoth7' returns whether at least one of them is even, or, both of them are 7. var output = isEitherEvenOrAreBoth7(3, 7); console.log(output); // --> false function isEitherEvenOrAreBoth7(num1, num2) { }

function isEitherEvenOrAreBoth7(num1, num2) { if ((num1 % 2 === 0)||(num2 % 2 === 0) || (num1 === 7) && (num2 === 7)) { return true; } else { return false; } }

Write a function called "isEqualTo". Given 2 numbers, "isEqualTo" returns whether num2 is equal to num1. var output = isEqualTo(11, 10); console.log(output); // --> false function isEqualTo(num1, num2) { }

function isEqualTo(num1, num2) { if (num1 === num2) { return true; } else { return false; } }

Write a function called "isEven". Given a number, "isEven" returns whether it is even. var output = isEven(11); console.log(output); // --> false function isEven(num) { }

function isEven(num) { if (num % 2 === 0) { return true; } else { return false; } }

Write a function called "isEvenAndGreaterThanTen". Given a number, "isEvenAndGreaterThanTen" returns whether it is both even and greater than 10. var output = isEvenAndGreaterThanTen(13); console.log(output); // --> false function isEvenAndGreaterThanTen(num) { }

function isEvenAndGreaterThanTen(num) { if (num % 2 === 0 && num > 10) { return true; } else { return false; } }

Write a function called "isEvenLength". Given a word, "isEvenLength" returns whether the length of the word is even. var output = isEvenLength('wow'); console.log(output); // --> false function isEvenLength(word) { }

function isEvenLength(word) { if (word.length % 2 === 0) { return true; } else { return false; } }

Write a function called "isEvenWithoutModulo". Given a number, "isEvenWithoutModulo" returns whether it is even. Notes: - It does so without using the modulo operator (%). - It should work for negative numbers and zero. var output = isEvenWithoutModulo(8); console.log(output); // --> true function isEvenWithoutModulo(num) { }

function isEvenWithoutModulo(num) { if (num === 0) { return true; } num = Math.abs(num); while (num >= 2) { num = num - 2; } if (num === 0){ return true; } else { return false; } }

Write a function called "isGreaterThan". Given 2 numbers, "isGreaterThan" returns whether num2 is greater than num1. var output = isGreaterThan(11, 10); console.log(output); // --> false function isGreaterThan(num1, num2) { }

function isGreaterThan(num1, num2) { if (num1 < num2) { return true; } else { return false; } }

Write a function called "isGreaterThan10". Given a number, "isGreaterThan10" returns whether the given number is greater than 10. var output = isGreaterThan10(11); console.log(output); // --> true function isGreaterThan10(num) { }

function isGreaterThan10(num) { if (num > 10) { return true; } else { return false; } }

Write a function called "isLessThan". Given 2 numbers, "isLessThan" returns whether num2 is less than num1. var output = isLessThan(9, 4); console.log(output); // --> true function isLessThan(num1, num2) { }

function isLessThan(num1, num2) { if (num1 > num2) { return true; } else { return false; } }

Write a function called "isLessThan30". Given a number, "isLessThan30" returns whether the given number is less than 30. var output = isLessThan30(9); console.log(output); // --> true function isLessThan30(num) { }

function isLessThan30(num) { if (num < 30) { return true; } else { return false; } }

Write a function called "isOdd". Given a number, "isOdd" returns whether the given number is odd. var output = isOdd(9); console.log(output); // --> true function isOdd(num) { }

function isOdd(num) { if (num % 2 === 1) { return true; } else { return false; } }

Write a function called "joinArrayOfArrays". Given an array of arrays, "joinArrayOfArrays" returns a single array containing the elements of the nested arrays. var output = joinArrayOfArrays([[1, 4], [true, false], ['x', 'y']]); console.log(output); // --> [1, 4, true, false, 'x', 'y'] You should be familiar with the "concat" method for this problem. function joinArrayOfArrays(arr) { }

function joinArrayOfArrays(arr) { var result = []; for (var i = 0; i < arr.length; i++){ result = result.concat(arr[i]); } return result; }

Write a function called "joinArrays". Given two arrays, "joinArrays" returns an array with the elements of "arr1" in order, followed by the elements in "arr2". var output = joinArrays([1, 2], [3, 4]); console.log(output); // --> [1, 2, 3, 4] You should be familiar with the "concat" method for this problem. function joinArrays(arr1, arr2) { }

function joinArrays(arr1, arr2) { return arr1.concat(arr2); }

Write a function called "joinThreeArrays". Given three arrays, "joinThreeArrays" returns an array with the elements of "arr1" in order followed by the elements in "arr2" in order followed by the elements of "arr3" in order. var output = joinThreeArrays([1, 2], [3, 4], [5, 6]); console.log(output); // --> [1, 2, 3, 4, 5, 6] Hint: do not use .join() function joinThreeArrays(arr1, arr2, arr3) { }

function joinThreeArrays(arr1, arr2, arr3) { return arr1.concat(arr2, arr3); }

Write a function called "keep". Given an array and a keeper element, "keep" returns an array containing the items that match the given keeper element. Notes: - If no elements match, "keep" should return an empty array. var output = keep([1, 2, 3, 2, 1], 2) console.log(output); --> [2, 2] function keep(array, keeper) { }

function keep(array, keeper) { for (var i = 0; i < array.length; i++) { if (array[i] !== keeper){ array.splice(i, 1); i--; } } return array; }

Write a function called "modulo". Given 2 numbers, "modulo" returns the remainder after dividing num1 by num2. Notes: - Do NOT use the actual built-in modulo (aka "remainder") operator (%) in your implementation. - 0 % ANYNUMBER = 0. - ANYNUMBER % 0 = NaN. - If either operand is NaN, then the result is NaN. = Modulo always returns the sign of the first number, even if the remainder is 0. var output = modulo(25, 4); console.log(output); // --> 1 }

function modulo(num1, num2) { if (num1 === 0){ return 0; } if (num2 === 0) { return NaN; } if( isNaN(num1) || isNaN(num2)) { return NaN; } var resultIsPositive = true; if (num1 < 0) { resultIsPositive = false; } num1 = Math.abs(num1); num2 = Math.abs(num2); while (num1 >= num2) { num1 = num1 - num2; } if (resultIsPositive) { return num1; } else { return -num1; } }

Write a function called "multiply". Given 2 numbers, "multiply" returns their product. Notes: - It should not use the multiply operator - * var output = multiply(4, 7); console.log(output); // --> 28 function multiply(num1, num2) { }

function multiply(num1, num2) { var resultIsPositive = true; if ((num1 > 0 && num2 < 0) || (num2 > 0 && num2 < 0)) { resultIsPositive = false; } num1 = Math.abs(num1); num2 = Math.abs(num2); var result = 0; for (var i = 0; i < num2; i++){ result += num1; } if (resultIsPositive) { return result; } else { return -result; } }

Write a function called "multiplyBetween". Given 2 integers, "multiplyBetween" returns the product between the two given integers, beginning at num1, and excluding num2. Notes: - The product between 1 and 4 is 1 * 2 * 3 = 6. - If num2 is not greater than num1, it should return 0. var output = multiplyBetween(2, 5); console.log(output); // --> 24 function multiplyBetween(num1, num2) { }

function multiplyBetween(num1, num2) { if (num2 <= num1) { return 0; } var product = 1; for (var i = num1; i < num2; i++) { product = product * i; } return product; }

Write a function called "or". Given 2 boolean expressions, "or" returns true or false, corresponding to the '||' operator. Notes: Do not use the || operator. Use ! and && operators instead. var output = or(true, false); console.log(output); // --> true;

function or(expression1, expression2) { return !(!expression1 && !expression2); }

Write a function called "removeArrayValues". Given an object, "removeArrayValues" removes any properties whose values are arrays. var obj = { a: [1, 3, 4], b: 2, c: ['hi', 'there'] } removeArrayValues(obj); console.log(obj); // --> { b: 2 } function removeArrayValues(obj) { }

function removeArrayValues(obj) { for (var key in obj) { if (Array.isArray(obj[key])){ delete obj[key]; } } }

Write a function called "removeElement". Given an array of elements, and a "discarder" parameter, "removeElement" returns an array containing the items in the given array that do not match the "discarder" parameter. Notes: - If all the elements match, it should return an empty array. - If an empty array is passed in, it should return an empty array. var output = removeElement([1, 2, 3, 2, 1], 2); console.log(output); // --> [1, 3, 1] function removeElement(array, discarder) { }

function removeElement(array, discarder) { for (var i = 0; i < array.length; i++) { if (array[i] === discarder) { array.splice(i, 1); i--; } } return array; }

Write a function called "removeEvenValues". Given an object, "removeEvenValues" removes any properties whose values are even numbers. Do this in place and return the original object, do not construct a cloned object that omits the properties. Example: var obj = { a: 2, b: 3, c: 4 }; removeEvenValues(obj); console.log(obj); // --> { b: 3 } function removeEvenValues(obj) { }

function removeEvenValues(obj) { for (var key in obj) { if (obj[key] % 2 === 0){ delete obj[key]; } } }

Write a function called "removeFromBack". Given an array, "removeFromBack" returns the given array with its last element removed. var output = removeFromBack([1, 2, 3]); console.log(output); // --> [1, 2] function removeFromBack(arr) { }

function removeFromBack(arr) { arr.pop(); return arr; }

Write a function called "removeFromFront". Given an array, "removeFromFront" returns the given array with its first element removed. var output = removeFromFront([1, 2, 3]); console.log(output); // --> [2, 3] function removeFromFront(arr) { }

function removeFromFront(arr) { arr.shift(); return arr }

Write a function called "removeNumberValues". Given an object, "removeNumberValues" removes any properties whose values are numbers. var obj = { a: 2, b: 'remaining', c: 4 }; removeNumberValues(obj); console.log(obj); // --> { b: 'remaining' } function removeNumberValues(obj) { }

function removeNumberValues(obj) { for (var key in obj) { if (typeof obj[key] === 'number'){ delete obj[key]; } } }

Write a function called "removeStringValuesLongerThan". Given an number and an object, "removeStringValuesLongerThan" removes any properties on the given object whose values are strings longer than the given number. var obj = { name: 'Montana', age: 20, location: 'Texas' }; removeStringValuesLongerThan(6, obj); console.log(obj); // { age: 20, location: 'Texas' } function removeStringValuesLongerThan(num, obj) { }

function removeStringValuesLongerThan(num, obj) { for (var key in obj) { if (obj[key].length > num) { delete obj[key]; } } }

Write a function called "repeatString". Given a string and a number, "repeatString" returns the given string repeated the given number of times. var output = repeatString('code', 3); console.log(output); // --> 'codecodecode' function repeatString(string, num) { }

function repeatString(string, num) { var result = ''; for (var i = 0; i < num; i++) { result += string; } return result; }

Write a function called "select". Given an array and an object, "select" returns a new object whose properties are those in the given object AND whose keys are present in the given array. Notes: If keys are present in the given array, but are not in the given object, it should ignore them. It does not modify the passed in object. var arr = ['a', 'c', 'e']; var obj = { a: 1, b: 2, c: 3, d: 4 }; var output = select(arr, obj); console.log(output); // --> { a: 1, c: 3 } function select(arr, obj) { }

function select(arr, obj) { var result = {}; for (var i = 0; i < arr.length; i++) { if (obj[arr[i]] !== undefined ) { result[arr[i]] = obj[arr[i]]; } } return result; }

Write a function called "square". Given a number, "square" should return the square of the given number. var output = square(5); console.log(output); // --> 25 function square(num) { }

function square(num) { return num ** 2; }

Write a function called "sumDigits". Given a number, "sumDigits" returns the sum of all its digits. var output = sumDigits(1148); console.log(output); // --> 14 If the number is negative, the first digit should count as negative. var output = sumDigits(-316); console.log(output); // --> 4 Notes: - In order to use some of the methods that will be most helpful to you, you will most likely want to do some string to number conversion and vice versa. - Be sure to familiarize yourself with the "toString" method, as well as the "Number" function. function sumDigits(num) { }

function sumDigits(num) { var inputIsNegative = false; if (num < 0) { num = Math.abs(num); inputIsNegative = true; } var resultNum = 0; var numString = num.toString(); var firstValue = Number(numString[0]); for (var i = 0; i < numString.length; i++) { resultNum += Number(numString[i]); } if(inputIsNegative) { resultNum = resultNum - (2* firstValue); return resultNum } else { return resultNum; } }

var dessert = { contents: "ice cream", price: "free" } function getProperty (obj, key) { return obj[key]; } var output = getProperty(dessert, 'content'); console.log(output); What would this code return?

ice cream

Dot notation: in the object.property syntax, the property must be a valid JavaScript ____________.

identifier

Bracket notation: in the object[property_name] syntax, the property_name is just a _______ or _________.

string; symbol

var dessert = { contents: "ice cream", price: "free" } function getProperty (obj, key) { return obj.key; } var output = getProperty(dessert, 'content'); console.log(output); What would this code return?

undefined (the reason is b/c the code written with dot notation is searching for a key actually spelled "key")


Conjuntos de estudio relacionados

Developmental Transitions I: Chapter 2 Diversity and Maternal Child Nursing

View Set

FAR- PARTNERSHIP AND CORPORATION

View Set

Chapter 1: Basic Geographic Concepts Chapter 2: North America

View Set

Overview of Cloud Computing and Amazon Web Services

View Set