Skip to content
14 changes: 14 additions & 0 deletions problems/.vscode/launch.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
// Use IntelliSense to learn about possible attributes.
// Hover to view descriptions of existing attributes.
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"type": "node",
"request": "launch",
"name": "Launch Program",
"program": "${file}"
}
]
}
26 changes: 19 additions & 7 deletions problems/problem1.js
Original file line number Diff line number Diff line change
@@ -1,27 +1,39 @@
// SOLVED!

var assert = require('assert');

// we need 5 test cases. I provided 1 input
let inputs = [
""
"",
"abc",
"cba",
"123",
"321"
]

let outputs = [

undefined,
"a",
"c",
"1",
"3"
]

// Make this function return the first letter of the string that is passed to it. If the string does not have a first letter, return undefined
function f(str) {

var array = str.split("");
var firstLetter = array[0];
return firstLetter;
}

function runTest(i) {
var expected = outputs[i];
var actual = f(inputs[i]);
assert.deepEqual(actual, expected);
var expected = outputs[i];
var actual = f(inputs[i]);
assert.deepEqual(actual, expected);
}

runTest(0);
runTest(1);
runTest(2);
runTest(3);
runTest(4);
runTest(4);
30 changes: 26 additions & 4 deletions problems/problem10.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,22 @@
// SOLVED!

var assert = require('assert');

// we need 5 test cases.
let inputs = [

"Hello world",
"Bonjour le monde",
"ola sénorita",
"hELLO eVERYONE iN tHE wORLD",
"Koalas are cool"
]

let outputs = [

"Hello World",
"Bonjour Le Monde",
"Ola Sénorita",
"Hello Everyone In The World",
"Koalas Are Cool"
]

/*
Expand All @@ -17,11 +27,23 @@ f("ALL YOUR BASE ARE BELONG"); // All Your Base Are Belong

*/
function f(str) {

var arr = str.toLowerCase().split(" ");
var firstChar = "";
var capWord = "";
var newArr = [];

for (var i = 0; i < arr.length; ++i) {
firstChar = arr[i].toUpperCase().charAt(0);
capWord = firstChar + arr[i].substring(1, arr[i].length);
newArr.push(capWord);
}

var newStr = newArr.join(" ");
return newStr;
}

function runTest(i) {
if(i > inputs.length) throw new Error("You do not have enough test cases");
if (i > inputs.length) throw new Error("You do not have enough test cases");
var expected = outputs[i];
var actual = f(inputs[i]);
assert.deepEqual(actual, expected);
Expand Down
28 changes: 24 additions & 4 deletions problems/problem11.js
Original file line number Diff line number Diff line change
@@ -1,23 +1,43 @@
// SOLVED!

var assert = require('assert');

// we need 5 test cases.
let inputs = [

[10, 3, 5, 3, 2],
[4, 3, 1, 6, 4, 2, 4],
[5, 3, 7, 9, 5, 6],
["hi", 4, 6, 3, 2, "", 5],
[]
]

let outputs = [

23,
24,
35,
20,
0
]

/*
Make this function return the sum of all the numbers in the input array. If any element in the array is not a number, skip it. If the array is empty, return zero.
*/
function f(arr) {

if (0 === arr.length) return 0;

for (var i = 0; i < arr.length; ++i) {
if ((isNaN(arr[i]) || arr[i] === ("" || ''))) {
arr.splice(i, 1, 0);
}
}

return arr.reduce(function (a, b) {
return a + b;
});
}

function runTest(i) {
if(i > inputs.length) throw new Error("You do not have enough test cases");
if (i > inputs.length) throw new Error("You do not have enough test cases");
var expected = outputs[i];
var actual = f(inputs[i]);
assert.deepEqual(actual, expected);
Expand Down
21 changes: 16 additions & 5 deletions problems/problem12.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,22 @@
// SOLVED!

var assert = require('assert');

// we need 5 test cases.
let inputs = [

[[1, 2, 3], [4, 5, 6]],
[[1, 2, 3, 4, 5, 6], [4, 5, 6]],
[[1, 2, 3], [1, 2, 3, 4, 5, 6]],
[[1, 3, 4], [1, 2, 3]],
[[1, 2, 3, 5], [2, 4, 5, 6]]
]

let outputs = [

[1, 2, 3, 4, 5, 6],
[1, 2, 3],
[4, 5, 6],
[4, 2],
[1, 3, 4, 6]
]

/*
Expand All @@ -19,12 +29,13 @@ uniqueElements([0,1,2,3], [1,3,4,5]); // [0,4,5]
uniqueElements([1,2,3], [1,2,3]); // []
uniqueElements(2,3); // undefined, not arrays
*/
function f(arr1, arr2) {

function f(arr) {
var newArr = !Array.isArray(arr[0]) && !Array.isArray(arr[1]) ? undefined : arr[0].concat(arr[1]).filter(e => arr[0].includes(e) && !arr[1].includes(e) || !arr[0].includes(e) && arr[1].includes(e));
return newArr;
}

function runTest(i) {
if(i > inputs.length) throw new Error("You do not have enough test cases");
if (i > inputs.length) throw new Error("You do not have enough test cases");
var expected = outputs[i];
var actual = f(inputs[i]);
assert.deepEqual(actual, expected);
Expand Down
29 changes: 25 additions & 4 deletions problems/problem13.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,22 @@
// SOLVED!

var assert = require('assert');

// we need 5 test cases.
let inputs = [

"racecar",
"butter",
"computer",
"kayak",
"bottom"
]

let outputs = [

true,
false,
false,
true,
false
]

/*
Expand All @@ -16,11 +26,22 @@ RADAR -> Yes
JAVASCRIPT -> No
*/
function f(str) {

var a = str;
var newStr = str.split('');
var newWord = [];
for (var i = 0; i <= str.length; ++i) {
newWord.push(newStr[str.length - i]);
}

var b = newWord.join('');

if (a == b) return true;

else return false;
}

function runTest(i) {
if(i > inputs.length) throw new Error("You do not have enough test cases");
if (i > inputs.length) throw new Error("You do not have enough test cases");
var expected = outputs[i];
var actual = f(inputs[i]);
assert.deepEqual(actual, expected);
Expand Down
43 changes: 37 additions & 6 deletions problems/problem14.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,22 @@
// SOLVED!

var assert = require('assert');

// we need 5 test cases.
let inputs = [

"01234567890123456789012345678901234567890123456789012345678901234567890123456789",
"0123456789 0123456789 0123456789 0123456789 0123456789 0123456789 0123456789",
"0123456789012345678901234567890123456789 0123456789012345678901234567890123456789",
" Second row",
"0123456789012345678901234567890123456789 9"
]

let outputs = [

"0123456789012345678901234567890123456789\n0123456789012345678901234567890123456789",
"0123456789 0123456789 0123456789 0123456\n789 0123456789 0123456789 0123456789",
"0123456789012345678901234567890123456789\n0123456789012345678901234567890123456789",
"Second row",
"0123456789012345678901234567890123456789\n9"
]

/*
Expand All @@ -30,12 +40,34 @@ Lorem ipsumos dolor sit amet consectetur

even though there is a space before the a in adipisicing
*/

// 1- Verifies is loop is necessary
// 2- Loop to change line at every 40 characters
// 3- Verifies if there is more text modify
// 4- Verifies if the first character of a new line would be an empty character
// 5- If that happens, it simply skips it and verifies the next character
// 6- No risk of this never ending or going to the end of the string because of str.trim() at beginning
// 7- Extracts 40 characters strating from the first index and then places the extracted string into the array's next position

function f(str) {

var newStr = str.trim();
if (newStr.length < 40) return newStr; // *1
var i = 0;
var j = 0;
var arr = [];
var endLoop = true;
while (endLoop === true) { // *2
if (newStr.length > (40 * i) + j) { // *3
if (newStr.charAt((40 * i) + j) === " ") while (newStr.charAt((40 * i) + j) === " ")++j; // *4*5*6
arr[i] = newStr.slice((40 * i) + j, (40 * (i + 1)) + j); // *7
}
else endLoop = false;
++i;
} return arr.join("\n");
}

function runTest(i) {
if(i > inputs.length) throw new Error("You do not have enough test cases");
if (i > inputs.length) throw new Error("You do not have enough test cases");
var expected = outputs[i];
var actual = f(inputs[i]);
assert.deepEqual(actual, expected);
Expand All @@ -45,5 +77,4 @@ runTest(0);
runTest(1);
runTest(2);
runTest(3);
runTest(4);

runTest(4);
43 changes: 37 additions & 6 deletions problems/problem15.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,22 @@
// SOLVED!

var assert = require('assert');

// we need 5 test cases.
let inputs = [

"01234567890123456789012345678901234567890123456789012345678901234567890123456789",
"0123456789 0123456789 0123456789 0123456789 0123456789 0123456789 0123456789",
"0123456789012345678901234567890123456789 0123456789012345678901234567890123456789",
" Second row",
"0123456789012345678901234567890123456789 9"
]

let outputs = [

"0123456789012345678901234567890123456789\n0123456789012345678901234567890123456789",
"0123456789 0123456789 0123456789 0123456\n789 0123456789 0123456789 0123456789",
"0123456789012345678901234567890123456789\n0123456789012345678901234567890123456789",
"Second row",
"0123456789012345678901234567890123456789\n9"
]

/*
Expand All @@ -30,12 +40,34 @@ Lorem ipsumos dolor sit amet consectetur

even though there is a space before the a in adipisicing
*/

// 1- Verifies is loop is necessary
// 2- Loop to change line at every 40 characters
// 3- Verifies if there is more text modify
// 4- Verifies if the first character of a new line would be an empty character
// 5- If that happens, it simply skips it and verifies the next character
// 6- No risk of this never ending or going to the end of the string because of str.trim() at beginning
// 7- Extracts 40 characters strating from the first index and then places the extracted string into the array's next position

function f(str) {

var newStr = str.trim();
if (newStr.length < 40) return newStr; // *1
var i = 0;
var j = 0;
var arr = [];
var endLoop = true;
while (endLoop === true) { // *2
if (newStr.length > (40 * i) + j) { // *3
if (newStr.charAt((40 * i) + j) === " ") while (newStr.charAt((40 * i) + j) === " ")++j; // *4*5*6
arr[i] = newStr.slice((40 * i) + j, (40 * (i + 1)) + j); // *7
}
else endLoop = false;
++i;
} return arr.join("\n");
}

function runTest(i) {
if(i > inputs.length) throw new Error("You do not have enough test cases");
if (i > inputs.length) throw new Error("You do not have enough test cases");
var expected = outputs[i];
var actual = f(inputs[i]);
assert.deepEqual(actual, expected);
Expand All @@ -45,5 +77,4 @@ runTest(0);
runTest(1);
runTest(2);
runTest(3);
runTest(4);

runTest(4);
Loading