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

Carina Taveras #170

Open
wants to merge 11 commits into
base: master
Choose a base branch
from
Open
102 changes: 92 additions & 10 deletions problems/arraysWithLoops.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,14 @@
* @returns {Array} - ["I!", "am!", "a!", "happy!", "array!"]
*/

function shoutForLoop() {}
function shoutForLoop(array){
let newArray = []
for(let i = 0; i < array.length; i++){
newArray.push(array[i] + "!")
}
return newArray
};

/**
* Takes in an array and returns a new array with element
* getting a ! added to the end.
Expand All @@ -15,7 +22,15 @@ function shoutForLoop() {}
* @return {Array} - ["I!", "am!", "a!", "happy!", "array!"]
*/

function shoutWhileLoop() {}
function shoutWhileLoop(array) {
let newArray = []
let i = 0
while (i < array.length){
newArray.push(array[i] + "!")
i++;
}
return newArray
}

/**
* Takes in an array and returns a new array with element
Expand All @@ -25,39 +40,75 @@ function shoutWhileLoop() {}
* @returns {Array} - ["I!", "am!", "a!", "happy!", "array!"]
*/

function shoutForOfLoop() {}
function shoutForOfLoop(array) {
let newArray = []
for (let i of array){
newArray.push(i + "!")
i++;

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

"for of" loops increment themselves so you don't need i++

also remember i isn't an index in a for of loop, it's the element, so this is like saying "happy"++

}
return newArray;
}

/**
* Takes in an array and returns the sum of all values
* @param {number[]} nums
* @returns {number} sum
*/

function sumArray() {}
const sumArray = (nums) => {
let sum = 0;
for(let i = 0; i < nums.length; i++){
sum += nums[i];
}
return sum;
}

/**
* Takes in an array and returns an array of all the odd valued elements
* @param {number[]} nums
* @returns {array} odds
*/

function oddArray() {}
function oddArray(nums) {
let newArray = [];
for (let i = 0; i < nums.length; i++){
if (nums[i] % 2 === 1){
newArray.push(nums[i])}
}
return newArray
}

/**
* Takes in an array and returns an array of all the even valued elements
* @param {number[]} nums
* @returns {array} evens
*/

function evenArray() {}
function evenArray(nums) {
let newArray = [];
for (let i = 0; i < nums.length; i++){
if (nums[i] % 2 === 0){
newArray.push(nums[i])
}
}
return newArray
}

/**
* Takes in array and returns the smallest number in the array
* @param {number[]} nums
* @returns {number} smallest value
*/

function findSmallest() {}
function findSmallest(nums) {
let smallestNum = nums[0];
for (let i = 0; i < nums.length; i++){
if (nums[i] < smallestNum){
smallestNum = nums[i]
}
}
return smallestNum
}

/**
* Takes in array and returns the second smallest number in the array
Expand All @@ -66,15 +117,38 @@ function findSmallest() {}
* @returns {number} second smallest value
*/

function findSecondSmallest() {}
function findSecondSmallest(nums) {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Think about how you can use your findSmallest function to find this answer

let sSmallest = Infinity
let smallest = Infinity
for(let i = 0; i < nums.length; i++){
if(nums[i] < smallest){
sSmallest = smallest
smallest = nums[i]
} else if (nums[i] < sSmallest && nums[i] > smallest){
sSmallest = nums[i]
}
} return sSmallest
}


/**
* Takes in array and returns the second largest number in the array
* @param {number[]} nums
* @returns {number} second largest value
*/

function findSecondLargest() {}
function findSecondLargest(nums) {
let largest = -Infinity
let sLargest = -Infinity
for(let i = 0; i < nums.length; i++){
if(nums[i] > largest){
sLargest = largest
largest = nums[i]
} else if(nums[i] > sLargest && nums[i] < largest){
sLargest = nums[i]
}
} return sLargest
}

/**
* Takes in array and returns an array with all the values but with no duplicates.
Expand All @@ -83,7 +157,15 @@ function findSecondLargest() {}
* @returns {array} nums without the duplicates
*/
// Hint: Look into the `.includes` method.
function removeDups() {}
function removeDups(nums) {
let newArr = []

for(let i = 0; i < nums.length; i++) {
if(!newArr.includes(nums[i])){
newArr.push(nums[i])
}
} return newArr
}

module.exports = {
shoutForLoop,
Expand Down
Loading