Created
June 7, 2017 20:35
-
-
Save vidjuheffex/4a245e40e2db578e2992e3fafd41a67c to your computer and use it in GitHub Desktop.
arrays and loops
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 1. | |
// Without logging the values, list the value of `sports` and `total` | |
var sports = ['soccer', 'baseball']; | |
var total = sports.push('football', 'swimming'); | |
// A: | |
// sports = ['soccer', 'baseball']; | |
// total = ['soccer', 'baseball', 'football', 'swimming'] | |
// 2. | |
// Using the `strings` array, wrte a function `longestString()` | |
// that accepts an array argument and returns the longest string in the array | |
var strings = ['this','is','a','collection','of','words']; | |
// A: | |
// array -> str | |
function longestString(arrayOfStrings){ | |
var longestWord = ""; | |
for (var x=0; x<arrayOfStrings.length; x++){ | |
var currentElement = arrayOfStrings[x]; | |
if (currentElement.length > longestWord.length) | |
longestWord = currentElement; | |
} | |
return longestWord; | |
} | |
console.assert(longestString(strings) === 'collection', {"message": "longestString should return 'collection'"}); | |
// Use the `numbers` array for questions 3 - 8. | |
var numbers = [1,12,4,18,9,7,11,3,101,5,6]; | |
// 3. | |
// Write a function `smallestNumber()` that accepts an array | |
// and returns the smallest number in the array. | |
// A: | |
// array -> number | |
function smallestNumber(arrayOfNumbers){ | |
var smallestNum = arrayOfNumbers[0]; | |
for (var x = 0; x < arrayOfNumbers.length; x++){ | |
var currentNum = arrayOfNumbers[x]; | |
if (currentNum < smallestNum){ | |
smallestNum = currentNum; | |
} | |
} | |
return smallestNum; | |
} | |
console.assert(smallestNumber(numbers) === 1, {"message": "smallestNumber should return 1"}); | |
// 4. | |
// Write a function `getEvens()` that accepts an array | |
// and returns only the even numbers in the array. | |
// A: | |
//array -> array | |
function getEvens(arrayOfNumbers){ | |
var evenArray = []; | |
for(var i = 0; i<arrayOfNumbers.length; i++){ | |
var currentElement = arrayOfNumbers[i]; | |
if (currentElement % 2 == 0){ | |
evenArray.push(currentElement); | |
} | |
} | |
//if the currentelement matches some condition (even), add it to evenArray | |
return evenArray; | |
} | |
console.assert(getEvens(numbers).toString() === '12,4,18,6', {'message': 'getEvens should return "12,4,18,6"'}); | |
// 5. | |
// Write a function that accepts an array argument | |
// and returns the array reversed | |
// Hint: When looping over the array, start at the last index | |
// and decrement the iterator to zero | |
// A: | |
//array -> array | |
function arrayReverser(arrayOfNumbers){ | |
var reversedArray = []; | |
//loop over everynumber starting at the end, | |
//push each number to the reversedArray; | |
for(var i=arrayOfNumbers.length-1 ; i>=0 ; i-- ){ | |
var currentElement = arrayOfNumbers[i]; | |
reversedArray.push(currentElement) | |
} | |
return reversedArray; | |
} | |
console.assert(arrayReverser(numbers).toString() === '6,5,101,3,11,7,9,18,4,12,1', {'message': 'arrayReverser should return "6,5,101,3,11,7,9,18,4,12,1"'}); | |
// 6. | |
// Write a function that accepts an array argument | |
// and returns the sum of all of the numbers in the array | |
// A: | |
//array -> number | |
function sumArrayOfNumbers(arrayOfNumbers){ | |
var sum = 0; | |
for (var i=0; i<arrayOfNumbers.length; i++){ | |
var currentElement = arrayOfNumbers[i]; | |
sum += currentElement; | |
} | |
return sum; | |
} | |
console.assert(sumArrayOfNumbers(numbers) === 177, {'message': 'sumArrayOfNumbers should return 177'}); | |
// 7. | |
// Write a function that accepts an array argument | |
// and returns an array of only the numbers greater than 10 | |
// A: | |
//array -> array | |
function numbersOver10(arrayOfNumbers){ | |
var numbersOver10Array= []; | |
for(var i =0; i< arrayOfNumbers.length; i++){ | |
var currentElement = arrayOfNumbers[i]; | |
if(currentElement > 10){ | |
numbersOver10Array.push(currentElement) | |
} | |
} | |
return numbersOver10Array; | |
} | |
console.assert(numbersOver10(numbers).toString() === "12,18,11,101", {'message': 'numbersOver10 should return "12,18,11,101"'}); | |
// 8. | |
// Write a function that accepts both an array and number argument | |
// and returns an array of only the numbers greater than the number passed to the function | |
// A: | |
// array, number -> array | |
function numbersOverX(arrayOfNumbers, num){ | |
var numbersOverXArray= []; | |
for(var i =0; i< arrayOfNumbers.length; i++){ | |
var currentElement = arrayOfNumbers[i]; | |
if(currentElement > num){ | |
numbersOverXArray.push(currentElement); | |
} | |
} | |
return numbersOverXArray; | |
} | |
console.assert(numbersOverX(numbers, 15).toString() === "18,101", {'message': 'numbersOverX should return "18,101"'}); | |
// 9. | |
// Write a function `joinArrays()` that takes an array of arrays, | |
// and returns a single array with the contents of the second array | |
// listed after the contents of the first array | |
var numbers = [1,12,4,18,9,7,11,3,101,5,6]; | |
var numbersTwo = [33,56,72,2,5,66,90,21,42]; | |
// A: | |
//array[array array] -> array | |
function joinArrays(arrayOfArrays){ | |
var combinedArray = arrayOfArrays[0].concat(arrayOfArrays[1]) | |
return combinedArray; | |
} | |
console.assert(joinArrays([numbers, numbersTwo]).toString() === '1,12,4,18,9,7,11,3,101,5,6,33,56,72,2,5,66,90,21,42', {'message': 'joinArrays should return "1,12,4,18,9,7,11,3,101,5,6,33,56,72,2,5,66,90,21,42"'}); | |
// 10. | |
// Using the `instructors` array, comment the value of each variable below | |
var instructors = [ | |
['JD','JavaScript'], | |
['Tim','JavaScript'], | |
['Brit','Ruby'], | |
['Joe','iOS'], | |
['Dan','JavaScript'], | |
['Will','JavaScript'], | |
['Calvin','JavaScript'], | |
['James','Ruby'] | |
]; | |
var instructorNameDiscipline = instructors[5]; | |
// greenvIlleInstructor = ['Will', 'Javascript'] | |
var instructorOne = instructors[4][0]; | |
// instructorOne = 'Dan' | |
var instructorTwo = instructors[0][1]; | |
// instructorTwo = 'Javascript' | |
var instructorThree = instructors[2][0]; | |
// instructorThree = 'Brit' |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment