Skip to content
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
21 changes: 17 additions & 4 deletions part01/BasicArrays.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,31 +4,44 @@ class BasicArrays{
* @return the first element in the array
*/
getFirstElement(stringArray) {
return null;
return stringArray[0];
}

/**
* @param stringArray an array of String objects
* @return the second element in the array
*/
getSecondElement(stringArray) {
return null;
return stringArray[1];
}

/**
* @param stringArray an array of String objects
* @return stringArray with the elements in reverse order
*/
reverse(stringArray) {
return null;
let newArray = [];
for(let i=stringArray.length - 1; i>=0; i--){
newArray.push(stringArray[i]);
}
return newArray;
}

/**
* @param stringArray an array of String objects
* @return String made up of the first character in each element of stringArray
*/
getFirstLetterOfEachElement(stringArray) {
return null;
let newArray = [];
let firstElement = [];

for (let i=0; i<=stringArray.length-1; i++){
newArray += stringArray[i][0];

}
firstElement.push(newArray);

return (firstElement.join(""));
}
}

Expand Down
4 changes: 2 additions & 2 deletions part01/BasicArrays.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ test("getSecondTest", () => {

let basicArrays = new BasicArrays();
let inputArray = ["The", "quick", "brown"];
let expected = "The";
let expected = "quick";

let actual = basicArrays.getSecondElement(inputArray);

Expand All @@ -38,7 +38,7 @@ test("reverseArrayTest", () => {
test("firstLetterTest", () => {

let basicArrays = new BasicArrays();
let inputArray = ["The", "quick", "brown"];
let inputArray = ["The", "quick", "brown", "fox"];
let expected = "Tqbf";

let actual = basicArrays.getFirstLetterOfEachElement(inputArray);
Expand Down
55 changes: 48 additions & 7 deletions part02/Craze4Arrays.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,31 @@
class Craze4Arrays{

getNumberOfOccurrences(objectArray, objectToEvaluate) {
let numOfOccurrences = 0;

for (let i = 0; i<objectArray.length; ++i ){
if (objectArray[i] == objectToEvaluate)
numOfOccurrences++;
}
return numOfOccurrences;
}



/**
@param objectArray an array of any type of Object
@param objectToRemove a value to be removed from the `objectArray`
@return an array with identical content excluding the specified `objectToRemove`
Given an array of objects, name `objectArray`, and an object `objectToRemove`, return an array of objects with identical contents excluding `objectToRemove`
*/
removeValue(objectArray, objectToRemove) {
return null;
for (let i=0; i<objectArray.length; i++){
if (objectArray[i] === objectToRemove){
objectArray.splice(i, 1);
i--;
}
}
return objectArray;
}

/**
Expand All @@ -16,16 +34,38 @@ class Craze4Arrays{
* given an array of objects, named `objectArray` return the most frequently occuring object in the array
*/
getMostCommon(objectArray) {
return null;
}
let m = 0;
let mf = 1;
let mode;
for (let i=0; i<objectArray.length; i++){
for (let j = i; j<objectArray.length; j++){
if (objectArray[i] == objectArray[j])
m++;
if (mf<m){
mf=m;
}
}
mode = mf;
m=0;
}

return mode;
}



/**
* @param objectArray an array of any type of Object
* @return the least frequently occurring object in the array
* given an array of objects, named `objectArray` return the least frequently occuring object in the array
*/
getLeastCommon(objectArray) {
return null;
getLeastCommon(objectArray){
let leastCommon = objectArray.sort((a,b) =>
objectArray.filter(v => v===b).length
- objectArray.filter(v => v===a).length
).pop();

return leastCommon;
}

/**
Expand All @@ -34,8 +74,9 @@ class Craze4Arrays{
* @return an array containing all elements in `objectArray` and `objectArrayToAdd`
* given two arrays `objectArray` and `objectArrayToAdd`, return an array containing all elements in `objectArray` and `objectArrayToAdd`
*/
pmergeArrays(objectArray, objectArrayToAdd) {
return null;
mergeArrays(objectArray, objectArrayToAdd) {
let mergedArray = objectArray.concat(objectArrayToAdd);
return mergedArray;
}

}
Expand Down
8 changes: 8 additions & 0 deletions part02/Craze4Arrays.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,14 @@ test("removeValueTest1", () => {
expect(actual).toEqual(expected);
});

test("getMostCommonTest", () => {
let craze4Arrays = new Craze4Arrays();
let expected = 4;
let inputArray = [1,1,2,3,3,3,4,4,4,4];
let actual = craze4Arrays.getMostCommon(inputArray);
expect(actual).toEqual(expected);
});

test("getLeastCommonTest", () => {
let craze4Arrays = new Craze4Arrays();
let expected = 2;
Expand Down