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

Assignment-1 JS Completed #370

Open
wants to merge 1 commit into
base: hkirat/with-tests
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
5 changes: 5 additions & 0 deletions 01-js/easy/anagram.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,12 @@
*/

function isAnagram(str1, str2) {
var st1=str1.toLowerCase();
var st2= str2.toLowerCase();

if(st1.split('').sort().join('') === st2.split('').sort().join('')) return true;

else return false;
}

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

function calculateTotalSpentByCategory(transactions) {
return [];

var categories={};

var output=[];

for(var item of transactions)
{
if(categories[item.category])
{
categories[item.category]=categories[item.category]+item.price
}

else
{
categories[item.category] = item.price;
}
}

for(var price in categories)
{
output.push({category: price, totalSpent: categories[price]});
}

return output;

}

module.exports = calculateTotalSpentByCategory;
55 changes: 54 additions & 1 deletion 01-js/hard/calculator.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,59 @@
- `npm run test-calculator`
*/

class Calculator {}
class Calculator {

constructor()
{
this.result=0;
}

add(num)
{
this.result=this.result+num;
}
subtract(num)
{
this.result=this.result-num;
}
multiply(num)
{
this.result=this.result*num;
}
divide(num)
{
if (num === 0) {
throw new Error("Cannot divide by zero");
}
this.result=this.result/num;
}
clear()
{
this.result=0;
}
getResult()
{
return this.result;
}
calculate(expression) {
expression = expression.replace(/\s+/g, '');

if (!/^[0-9+\-*/().]+$/.test(expression)) {
throw new Error("Invalid expression");
}

try {
// Check for division by zero
if (expression.includes('/0')) {
throw new Error("Cannot divide by zero");
}

this.result = eval(expression);
} catch (error) {
throw new Error("Invalid expression");
}
}

}

module.exports = Calculator;
33 changes: 33 additions & 0 deletions 01-js/hard/todo-list.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,39 @@

class Todo {

constructor()
{
this.todos=[];
}

add(todo)
{
this.todos.push(todo);
}

remove(ind)
{
if(ind>=0 && ind< this.todos.length) this.todos.splice(ind,1);
}

update(ind,newTodo)
{
if(ind>=0 && ind< this.todos.length) this.todos[ind]= newTodo;
}
getAll()
{
return this.todos;
}
get(ind)
{
if(ind>=0 && ind< this.todos.length) return this.todos[ind];

return null;
}
clear()
{
this.todos=[];
}
}

module.exports = Todo;
17 changes: 17 additions & 0 deletions 01-js/medium/palindrome.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,23 @@
*/

function isPalindrome(str) {
var lstr=str.toLowerCase();
lstr = lstr.replaceAll(" ","");

var n=lstr.length;
var i=0,j=n-1;

while(i<j)
{
if(lstr[i]<'a' || lstr[i]>'z')i++;
if(lstr[j]<'a' || lstr[j]>'z')j--;

if(lstr[i]!= lstr[j]) return false;

i++;
j--;
}

return true;
}

Expand Down
15 changes: 13 additions & 2 deletions 01-js/medium/times.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,16 @@ Hint - use Date class exposed in JS
*/

function calculateTime(n) {
return 0.01;
}
var sum=0;

var curr= parseFloat(Date.now());
for(var i=1; i<=n; i++)
{
sum+=i;
}
var ncurr= parseFloat(Date.now());

return parseFloat(ncurr-curr);
}

console.log(calculateTime(10000000));