Skip to content

Commit 72c430e

Browse files
author
wakidurrahman
committed
feat: Sum of Array Plus One
1 parent ce5d33f commit 72c430e

File tree

2 files changed

+82
-58
lines changed

2 files changed

+82
-58
lines changed

src/code-challenges/bubble-sort.js

Lines changed: 2 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
/**
2-
* 001: Bubble Sort: is based on the idea of repeatedly comparing pairs of adjacent elements and
2+
* 001: Explain How Bubble Sort works
3+
* Bubble Sort: is based on the idea of repeatedly comparing pairs of adjacent elements and
34
* then swapping their positions if they are in the wrong order.
45
* Bubble sort is a stable, in-place sort algorithm.
56
*/
@@ -46,54 +47,3 @@ const bubbleSortVariationRecursive = function (array, pointer = array.length - 1
4647
};
4748

4849
bubbleSortVariationRecursive([6, 5, 3, 1, 8, 7, 2, 4]);
49-
50-
/**
51-
* Lucky Sevens
52-
* 002: Write a function called lucky_sevens which takes an array of integers and returns true if any three consecutive elements sum to 7.
53-
*/
54-
55-
function luckySevensVariationOne(arrayOfIntegers) {
56-
// Array length
57-
const len = arrayOfIntegers.length;
58-
// If array of length is less than 3 elements then this challenge is not possible.
59-
if (len < 3) {
60-
return 'Not possible';
61-
}
62-
63-
// Because we know there are at least 3 elements we can
64-
// Start the loop at the 3rd element in the array (i = 2);
65-
// and check it along with the two previous elements (i - 1) and (i - 2)
66-
67-
for (let i = 0; i < len; i++) {
68-
if (arrayOfIntegers[i] + arrayOfIntegers[i - 1] + arrayOfIntegers[i - 2] === 7) {
69-
return true;
70-
}
71-
}
72-
73-
// if loop is finished and no elements summed to 7;
74-
return false;
75-
}
76-
77-
luckySevensVariationOne([2, 1, 5, 1, 0]);
78-
79-
function luckySevensVariationTwo(array) {
80-
const len = array.length;
81-
82-
// Iterate through the array.
83-
84-
for (let i = 0; i < len - 2; i++) {
85-
let sum = array[i] + array[i + 1] + array[i + 2];
86-
87-
if (sum === 7) {
88-
return true; // Found three consecutive elements that sum to 3;
89-
}
90-
}
91-
92-
return false; // No three consecutive elements sum to 7;
93-
}
94-
95-
let numbersOfLuckySeven = [1, 2, 3, 4, 5, 6, 1];
96-
console.log(luckySevensVariationTwo(numbers)); // Output: true
97-
98-
var numbers2OfLuckySeven = [1, 2, 3, 4, 5, 6, 2];
99-
console.log(luckySevensVariationTwo(numbers2OfLuckySeven)); // Output: false

src/code-challenges/code-challenges-003-010.js renamed to src/code-challenges/code-challenges-002-010.js

Lines changed: 80 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,57 @@
1+
/**
2+
* 002: Lucky Sevens
3+
* Problem: Write a function called lucky_sevens which takes an array of integers and returns true if any three consecutive elements sum to 7.
4+
*/
5+
6+
function luckySevensVariationOne(arrayOfIntegers) {
7+
// Array length
8+
const len = arrayOfIntegers.length;
9+
// If array of length is less than 3 elements then this challenge is not possible.
10+
if (len < 3) {
11+
return 'Not possible';
12+
}
13+
14+
// Because we know there are at least 3 elements we can
15+
// Start the loop at the 3rd element in the array (i = 2);
16+
// and check it along with the two previous elements (i - 1) and (i - 2)
17+
18+
for (let i = 0; i < len; i++) {
19+
if (arrayOfIntegers[i] + arrayOfIntegers[i - 1] + arrayOfIntegers[i - 2] === 7) {
20+
return true;
21+
}
22+
}
23+
24+
// if loop is finished and no elements summed to 7;
25+
return false;
26+
}
27+
28+
luckySevensVariationOne([2, 1, 5, 1, 0]);
29+
30+
function luckySevensVariationTwo(array) {
31+
const len = array.length;
32+
33+
// Iterate through the array.
34+
35+
for (let i = 0; i < len - 2; i++) {
36+
let sum = array[i] + array[i + 1] + array[i + 2];
37+
38+
if (sum === 7) {
39+
return true; // Found three consecutive elements that sum to 3;
40+
}
41+
}
42+
43+
return false; // No three consecutive elements sum to 7;
44+
}
45+
46+
let numbersOfLuckySeven = [1, 2, 3, 4, 5, 6, 1];
47+
console.log(luckySevensVariationTwo(numbers)); // Output: true
48+
49+
var numbers2OfLuckySeven = [1, 2, 3, 4, 5, 6, 2];
50+
console.log(luckySevensVariationTwo(numbers2OfLuckySeven)); // Output: false
51+
152
/**
253
* Q003: Simple clock angle
3-
* Problem
4-
* You will be given a number N that represents where the minute hand currently is on a clock.
54+
* Problem: You will be given a number N that represents where the minute hand currently is on a clock.
555
* Your program should return the angle that is formed by the minute hand and the 12 o'clock mark on the clock.
656
*/
757

@@ -15,8 +65,7 @@ function simpleClockAngle(number) {
1565

1666
/**
1767
* Q004: Sum of several arrays
18-
* Problem
19-
* You will be given an array of several arrays that each contain integers and your goal is to write a function that will sum up all the numbers in all the arrays.
68+
* Problem: You will be given an array of several arrays that each contain integers and your goal is to write a function that will sum up all the numbers in all the arrays.
2069
* For example, if the input is [[3, 2], [1], [4, 12]] then your program should output 22 because 3 + 2 + 1 + 4 + 12 = 22. Solve without and with reduce.
2170
*/
2271

@@ -45,7 +94,7 @@ multiDimensionalSumOfSeveralArrayVariationReduce([[3, 2], [1], [4, 12]]);
4594

4695
/**
4796
* Q005: Test divisors of three
48-
* You will be given 2 parameters: a low and high number.
97+
* Problem: You will be given 2 parameters: a low and high number.
4998
* Your goal is to print all numbers between low and high, and for each of these numbers print whether or not the number is divisible by 3.
5099
* If the number is divisible by 3, print the word "div3" directly after the number.
51100
*/
@@ -71,7 +120,7 @@ testDivisors(2, 10);
71120

72121
/**
73122
* Q006: Oddball sum
74-
* Write a function called oddball_sum which takes in a list of numbers and returns the sum of all the odd elements.
123+
* Problem: Write a function called oddball_sum which takes in a list of numbers and returns the sum of all the odd elements.
75124
* Try to solve with and without reduce function.
76125
*
77126
*/
@@ -106,3 +155,28 @@ function oddBallSumVariationReduce(numbs) {
106155
}
107156

108157
oddBallSumVariationReduce([1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15]);
158+
159+
/**
160+
* Q007: Sum of Array Plus One
161+
*
162+
* Problem: Write a function that takes an array of integers and returns the sum of the integers after adding 1 to each.
163+
*
164+
*/
165+
166+
// ES5 method is nice and clean
167+
exports.es5 = function (array) {
168+
return array.reduce((memo, num) => {
169+
return memo + num;
170+
}, array.length);
171+
};
172+
173+
// Without array.reduce method isn't much different
174+
exports.iterative = function (array) {
175+
let result = array.length;
176+
177+
for (let i = 0; i < array.length; i++) {
178+
result += array[i];
179+
}
180+
181+
return result;
182+
};

0 commit comments

Comments
 (0)