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

Update array.js #7

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
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
40 changes: 34 additions & 6 deletions src/array.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,10 @@
* [0, 1, 2, 3, 4, 5], 5 => 5
*/
function findElement(arr, value) {
throw new Error("Not implemented");
for (var i = 0; i<arr.length; i++)
if (value == arr[i])
return i;
return -1;
}

/**
Expand All @@ -27,7 +30,11 @@ function findElement(arr, value) {
* [] => []
*/
function doubleArray(arr) {
throw new Error("Not implemented");
var newArr = [];
for (var i = 1; i<3; i++)
for (var j = 0; j<arr.length; j++)
newArr[i*arr.length+j] = arr[j];
return newArr;
}

/**
Expand All @@ -42,7 +49,13 @@ function doubleArray(arr) {
* [] => []
*/
function getArrayOfPositives(arr) {
throw new Error("Not implemented");
var newArr = [];
var count = 0;
arr.forEach(element => {
if (element>0)
newArr[count++] = element;
});
return newArr;
}

/**
Expand All @@ -59,7 +72,13 @@ function getArrayOfPositives(arr) {
* [ false, 0, NaN, '', undefined ] => [ ]
*/
function removeFalsyValues(arr) {
throw new Error("Not implemented");
var newArr = [];
var count = 0;
arr.forEach(element => {
if (element == true || element.length != 0)
newArr[count++] = element;
})
return newArr;
}

/**
Expand All @@ -73,7 +92,12 @@ function removeFalsyValues(arr) {
* [ 'angular', 'react', 'ember' ] => [ 7, 5, 5 ]
*/
function getStringsLength(arr) {
throw new Error("Not implemented");
var newArr = [];
var count = 0;
arr.forEach(element => {
newArr[count++] = element.length;
})
return newArr;
}

/**
Expand All @@ -89,7 +113,11 @@ function getStringsLength(arr) {
* [ 1, 10, 100, 1000 ] => 1111
*/
function getItemsSum(arr) {
throw new Error("Not implemented");
var sum = 0;
arr.forEach(element => {
sum += element;
})
return sum;
}

module.exports = {
Expand Down