Skip to content

Commit

Permalink
Added algorithm problems
Browse files Browse the repository at this point in the history
  • Loading branch information
Jason011125 committed Mar 23, 2022
1 parent 1186a90 commit 090ed2b
Show file tree
Hide file tree
Showing 52 changed files with 218 additions and 0 deletions.
26 changes: 26 additions & 0 deletions BasicAlgorithmAndDataStructures/IterateThroughKeysOfAnObj.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
const users = {
Alan: {
online: false
},
Jeff: {
online: true
},
Sarah: {
online: false
}
}

function countOnline(usersObj) {

// Only change code below this line
let count = 0;
for (let user in usersObj) {
if (usersObj[user].online == true) {
count++;
}
}
return count;
// Only change code above this line
}

console.log(countOnline(users));
28 changes: 28 additions & 0 deletions BasicAlgorithmAndDataStructures/ModifyArrayStoredInObjs.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
let user = {
name: 'Kenneth',
age: 28,
data: {
username: 'kennethCodesAllDay',
joinDate: 'March 26, 2016',
organization: 'freeCodeCamp',
friends: [
'Sam',
'Kira',
'Tomo'
],
location: {
city: 'San Francisco',
state: 'CA',
country: 'USA'
}
}
};

function addFriend(userObj, friend) {
// Only change code below this line
userObj.data.friends.push(friend);
return userObj.data.friends;
// Only change code above this line
}

console.log(addFriend(user, 'Pete'));
27 changes: 27 additions & 0 deletions BasicAlgorithmAndDataStructures/ObjectKeys.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
let users = {
Alan: {
age: 27,
online: false
},
Jeff: {
age: 32,
online: true
},
Sarah: {
age: 48,
online: false
},
Ryan: {
age: 19,
online: true
}
};

function getArrayOfUsers(obj) {
// Only change code below this line
let Newarr = Object.keys(obj);
return Newarr;
// Only change code above this line
}

console.log(getArrayOfUsers(users));
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
21 changes: 21 additions & 0 deletions InsertIndex.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
function CompareForSort(first, second) {
if (first == second)
return 0;
if (first < second)
return -1;
else
return 1;
}
function getIndexToIns(arr, num) {
arr.sort(CompareForSort);
for (let i = 0; i < arr.length; i++) {
if (arr[i] == num) {
return i;
}
if (arr[i] > num) {
return i;
}

}
return arr.length;
}
11 changes: 11 additions & 0 deletions JavascriptAlgorithmScripting/BounceOffFalsyValues.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
function bouncer(arr) {
let newarr = [];
for (let i = 0; i < arr.length; i++) {
if (arr[i]) {
newarr.push(arr[i]);
}
}
return newarr;
}

console.log(bouncer([7, "ate", "", false, 9]));
6 changes: 6 additions & 0 deletions JavascriptAlgorithmScripting/CelsiusToFahrenheit.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
function convertToF(celsius) {
let fahrenheit = 9 / 5 * celsius + 32;
return fahrenheit;
}

convertToF(30);
6 changes: 6 additions & 0 deletions JavascriptAlgorithmScripting/ConfirmEnding.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
function confirmEnding(str, target) {
let regex = new RegExp(target + "$", "i");
return regex.test(str);
}

console.log(confirmEnding("Bastian", "n"));
10 changes: 10 additions & 0 deletions JavascriptAlgorithmScripting/Factorial.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
function factorialize(num) {
let res = 1;
while (num >= 1) {
res = res * num;
num--;
}
return res;
}

factorialize(5);
13 changes: 13 additions & 0 deletions JavascriptAlgorithmScripting/FindLongestString.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
function findLongestWordLength(str) {
let strArr = str.split(' ');
console.log(strArr);
let LongestString = 0;
for (let index = 0; index < strArr.length; index++) {
if (strArr[index].length > LongestString) {
LongestString = strArr[index].length;
}
}
return LongestString;
}

findLongestWordLength("The quick brown fox jumped over the lazy dog");
10 changes: 10 additions & 0 deletions JavascriptAlgorithmScripting/FixFormatOfTitle.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
function titleCase(str) {
let strArr = str.split(' ');
for (let index = 0; index < strArr.length; index++) {
strArr[index] = strArr[index].toLowerCase();
strArr[index] = strArr[index].charAt(0).toUpperCase() + strArr[index].slice(1);
}
return strArr.join(' ');
}

console.log(titleCase("sHoRt AnD sToUt"));
13 changes: 13 additions & 0 deletions JavascriptAlgorithmScripting/RepeatNum.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
function repeatStringNumTimes(str, num) {
if (num <= 0) {
return '';
}
let newStr = [];
while (num >= 1) {
newStr.push(str);
num--;
}
return newStr.join('');
}

console.log(repeatStringNumTimes("abc", 3))
17 changes: 17 additions & 0 deletions JavascriptAlgorithmScripting/ReturnLargestNumInArrays.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
function largestOfFour(arr) {
let finalarr = [];
for (let i = 0; i < arr.length; i++) {
let largest = arr[i][0];
for (let arrIndex = 0; arrIndex < arr[i].length; arrIndex++) {
if (arr[i][arrIndex] > largest) {
largest = arr[i][arrIndex];
}
}
console.log(largest);
finalarr.push(largest);

}
return finalarr;
}

console.log(largestOfFour([[17, 23, 25, 12], [25, 7, 34, 48], [4, -10, 18, 21], [-72, -3, -17, -10]]));
7 changes: 7 additions & 0 deletions JavascriptAlgorithmScripting/ReverseString.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
function reverseString(str) {
let strArr = str.split("");
strArr = strArr.reverse();
str = strArr.join('');
return str;
}

10 changes: 10 additions & 0 deletions JavascriptAlgorithmScripting/TruncateString.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
function truncateString(str, num) {
let Strarr = str.split('');
let resarr = Strarr.slice(0, num);
if (num < Strarr.length) {
resarr.push("...");
}
return resarr.join('');
}

console.log(truncateString("A-tisket a-tasket A green and yellow basket", 8));
5 changes: 5 additions & 0 deletions JavascriptAlgorithmScripting/checkBoolean.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
function booWho(bool) {
return typeof (bool) == "boolean";
}

booWho(null);
8 changes: 8 additions & 0 deletions JavascriptAlgorithmScripting/spliceArrsWithoutChanging.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
function frankenSplice(arr1, arr2, n) {
let arr3 = arr2.slice();
for (let i = 0; i < arr1.length; i++) {
arr3.splice(n + i, 0, arr1[i]);
}
return arr3;
}
console.log(frankenSplice([1, 2, 3], [4, 5], 1));

0 comments on commit 090ed2b

Please sign in to comment.