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

01 js assignments #1031

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
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
17 changes: 16 additions & 1 deletion 01-js/easy/anagram.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,22 @@
*/

function isAnagram(str1, str2) {

let asciiSumStr1 = 0;
let asciiSumStr2 = 0;
const string1 = str1.toLowerCase().trim();
const string2 = str2.toLowerCase().trim();
if (string1.length !== string2.length) {
return false;
}
for (let i = 0; i < string1.length; i++) {
asciiSumStr1 = asciiSumStr1 + string1.charCodeAt(i);
asciiSumStr2 = asciiSumStr2 + string2.charCodeAt(i);
}
if (asciiSumStr1 === asciiSumStr2) {
return true;
} else {
return false;
}
}

module.exports = isAnagram;
20 changes: 19 additions & 1 deletion 01-js/easy/expenditure-analysis.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,25 @@
*/

function calculateTotalSpentByCategory(transactions) {
return [];
const spent = [];
const catgeoryDetails = {};
transactions.forEach((element) => {
if (catgeoryDetails[element.category] === undefined) {
catgeoryDetails[element.category] = element.price;
} else {
let totalPrice = catgeoryDetails[element.category];
totalPrice = totalPrice + element.price;
catgeoryDetails[element.category] = totalPrice;
}
});
for (const key in catgeoryDetails) {
const spentObject = {
category: key,
totalSpent: catgeoryDetails[key],
};
spent.push(spentObject);
}
return spent;
}

module.exports = calculateTotalSpentByCategory;
10 changes: 8 additions & 2 deletions 01-js/easy/findLargestElement.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,13 @@
*/

function findLargestElement(numbers) {

let largestElem = numbers[0];
numbers.forEach((element) => {
if (element > largestElem) {
largestElem = element;
}
});
return largestElem;
}

module.exports = findLargestElement;
module.exports = findLargestElement;
13 changes: 11 additions & 2 deletions 01-js/medium/countVowels.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,16 @@
*/

function countVowels(str) {
// Your code here
// Your code here
const convertedString = str.toLowerCase();
let vowelCount = 0;
const vowels = ["a", "e", "i", "o", "u"];
for (let i = 0; i < convertedString.length; i++) {
if (vowels.includes(convertedString[i])) {
vowelCount = vowelCount + 1;
}
}
return vowelCount;
}

module.exports = countVowels;
module.exports = countVowels;
20 changes: 19 additions & 1 deletion 01-js/medium/palindrome.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,25 @@
*/

function isPalindrome(str) {
return true;
let isPalindrome = false;
// level
let reverse = "";
for (let i = str.length - 1; i >= 0; i--) {
if (str[i].toLowerCase() === "?" || str[i].toLowerCase() === "!") {
continue;
} else {
reverse = reverse + str[i].toLowerCase();
}
}
console.log("reverse", reverse);
if (str.toLowerCase() === reverse) {
isPalindrome = true;
} else {
isPalindrome = false;
}

return isPalindrome;
}

module.exports = isPalindrome;
isPalindrome("Eva, can I see bees in a cave?");
6 changes: 6 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.