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
10 changes: 5 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
# data-structures-and-algorithms
# Challenge Summary
<!-- Short summary or background information -->

a function which takes in an array and the value and
## Challenge Description
<!-- Description of the challenge -->

## Approach & Efficiency
<!-- What approach did you take? Why? What is the Big O space/time for this approach? -->
a function called insertShiftArray which takes in an array and the value to be added .

## Approach & Efficiency
a function which takes in an array and the value to be added then return an array with the new value added at the middle index
## Solution
<!-- Embedded whiteboard image -->
![uml](https://github.com/Goorob-401-advanced-javascript/data-structures-and-algorithms/blob/array-shiftt/assets/array-shift.jpg)
12 changes: 12 additions & 0 deletions __test__/array-shift.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
'use strict';
const validatior = require('../challange/array-shift/array-shift.js')
describe('Validator Module', () => {
it('validate if return a new array that added the value to it middle ', () => {
let arr = validatior([3, 4, 5, 6, 8, 7], 6);
expect(arr[3]).toEqual(6)
})
})

it('validate the array length after adding ', () => {
expect(validatior([1, 2, 3, 4, 5], 6).length).toEqual(6)
})
Binary file added assets/array-shift.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
14 changes: 14 additions & 0 deletions challange/array-shift/array-shift.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
'use strict';

function insertShiftArray(arr, val) {
let mid = Math.floor(arr.length / 2);
let newArr = new Array(arr.length + 1);
for (let i = 0; i <= mid; i++) {
if (i === mid) { newArr[i] = val; }
else { newArr[i] = arr[i]; }
}
for (let j = mid; j <= arr.length - 1; j++) {
newArr[j + 1] = arr[j];
} return newArr;
}
module.exports = insertShiftArray;
Loading