Skip to content
Open
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
99 changes: 91 additions & 8 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,24 @@
//iteratee is a function that must return something, capture whatever it returns in a variable
//add the returned value from iteratee tp myNewArray
//after looping, return myNewArray
function map(array, iteratee){

let arr = [2,9,1,2,3,4,5,13];

function addFive (i) {
return i+5
}

function map(array, iteratee){
let myNewArray = [];
for (let i=0; i<array.length; i++) {
let newVar = iteratee(array[i])
myNewArray.push(newVar);
}
return myNewArray;
}

console.log(map(arr, addFive))

//https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/filter
//create a function called `filter`, it should take 2 parameters `array` and `iteratee`
//`array` must be an array
Expand All @@ -24,47 +38,100 @@ function map(array, iteratee){
// passing in the item from the current loop
//iteratee will return true or false, if true add the item to myNewArray else do not
//after looping, return myNewArray
function filter(array, iteratee){

function removeThree (i) {
if(i>=3) {
return true
}
}

function filter(array, iteratee) {
let myNewArray = [];
for (let i=0; i<array.length; i++) {
let j = array[i];
if (iteratee(array[i])) {
myNewArray.push(j);
}
}
return myNewArray;
}

console.log(filter(arr, removeThree))

//https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/find
//create a function called `find`, it should take 2 parameters `theArray` and `fnc`
//loop theArray and call the fnc for each thing in the array,
// passing in the item from the current loop
//fnc will return true or false, if true return the item
//after looping, return null
function find(theArray, fnc){

function thingy(x) {
if (x>4) {
return true;
}
}

function find(theArray, fnc){
for (let i=0; i<theArray.length; i++) {
if (fnc(theArray[i])) {
console.log(theArray[i]);
break;
}
}
}

find(arr, thingy);

//return the last item in theArray
function findLast(theArray){

let lastOne = theArray[theArray.length-1];
console.log(lastOne);
}

findLast(arr);

//return the first element of the array
function head(theArray){

let firstOne = theArray[0]
console.log(firstOne);
}

head(arr);

//create a new array
//loop theArray in reverse order
//add the item from each loop to the new array
//return the new array
function reverse(theArray){

newArr = [10, 15, 20, 25, 30, 35];

function reverse(theArray){
let reversedArr = [];
// while (theArray.length != 0) {
// reversedArr.push(theArray.pop());
// }
for (let i=theArray.length-1; i>=0; i--) {
reversedArr.push(theArray[i])
}
console.log(reversedArr);
}

reverse(newArr);

//create a new array
//loop theArray
//add the item from each loop to the new array except the first item
//return the new array
function tail(theArray){

let newArr = [];
for (let i=1; i<theArray.length; i++) {
newArr.push(theArray[i]);
}
console.log(newArr);
}

tail(newArr);

//implement the most basic sorting algorithm there is
//assume the array will always have numbers
//use a while loop to constantly loop theArray until it is sorted
Expand All @@ -75,10 +142,26 @@ function tail(theArray){
//if a swap is done set it to true
//after each for loop check the variable, if true, continue the while loop
//if false return theArray
function sort(theArray){

let yarra = [23,4,6,56,8,13,98,3,4,78]

function sort(theArray){

for (let i=0; i<theArray.length-1; i++) {
while (theArray[i]>theArray[i+1]) {
theArray.splice([i],2,theArray[i+1],theArray[i]);
for (let j=1; j<theArray.length+1; j++) {
if (theArray[i]>theArray[j]) {
i=0
}
}
}
}
console.log(theArray);
}

sort(yarra);

exports.map = map;
exports.filter = filter;
exports.find = find;
Expand Down