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

easy week 1 completed #251

Open
wants to merge 7 commits into
base: main
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
1 change: 1 addition & 0 deletions kiran.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
I am the greatest coder to ever exist
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.

14 changes: 14 additions & 0 deletions week-1/Week-1-assignment-with-tests/01-js/easy/anagram.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,23 @@
Once you've implemented the logic, test your code by running
- `npm run test-anagram`
*/
function sort(str){
var array=str.split("")
array.sort()
var sortedString=array.join("")
return sortedString
}

function isAnagram(str1, str2) {
if(sort(str1)==sort(str2)){
return true;
}
else{
return false
}

}

console.log(isAnagram("bass","ssab"));

module.exports = isAnagram;
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,46 @@
*/

function calculateTotalSpentByCategory(transactions) {
return [];
spendEstimates=[];
for(var i=0;i<transactions.length;i++){
var t=transactions[i];
if(spendEstimates[t.category]){
spendEstimates[t.category]=spendEstimates[t.category]+t.price;
}
else{
spendEstimates[t.category]=t.price;
}

}
console.log(spendEstimates);
}

transactions=[{
itemName:"Shawarma",
category:"Food",
price:80,
timestamp: "08-12-23"
},
{
itemName:"Mountain Dew",
category:"Drink",
price:25,
timestamp: "09-12-23"
},
{
itemName:"Masala Dosa",
category:"Food",
price:45,
timestamp: "11-12-23"
},
{
itemName:"Badam Milk",
category:"Drink",
price:20,
timestamp: "12-12-23",


}];
console.log(calculateTotalSpentByCategory(transactions));

module.exports = calculateTotalSpentByCategory;
87 changes: 86 additions & 1 deletion week-1/Week-1-assignment-with-tests/01-js/hard/calculator.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,91 @@
- `npm run test-calculator`
*/

class Calculator {}
class Calculator {
constructor(){
this.result=0;
}
addition(num){
this.result+=num;
}
subtraction(num){
this.result-=num;
}
multiply(num){
this.result*=num;
}
divide(num){
if(num===0){
console.log("Can't divide a number with zero!");

}
this.result/=num;
}
clear(){
this.result=0;
}
getResult(){
return this.result;
}
calculate(expression){
const sanitizedExpression=expression.replace(/\s+/g,' ');
const numericRegex=/^[+-]?\d+(\.\d+)?$/;

const token=[];
let currentToken='';

for(let char of sanitizedExpression){
if(char.match(/[0-9.]|\+|\-|\*|\//)){
currentToken+=char;
}
else{
if(currentToken){
if(!currentToken.match(numericRegex)){
throw new Error('Invalid input:Non numerical value detected');
}
tokens.push(Number(currentToken));
currentToken='';
}

if(char!==''){
tokens.push(char);
}
}
}

if(currentToken){
if(!currentToken.match(numericRegex)){
throw new Error('Invalid input:Non-numerical value detected');
}
token.push(Number(currentToken))
}

const stack=[];
let operator='+';

for(let token of tokens){
if(typeof token==='number'){
if(operator==='+'){
this.addition(token);
}else if(operator==='-'){
this.subtraction(token);
}else if(operator==='*'){
this.multiply(token);
}else if(operator==='/'){
this.divide(token);
}


}else if(['+','-','*','/'].includes(token)){
operator=token;
}

}

return this.result;

}

}

module.exports = Calculator;
61 changes: 60 additions & 1 deletion week-1/Week-1-assignment-with-tests/01-js/hard/todo-list.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,67 @@
- `npm run test-todo-list`
*/

class Todo {
class Todo{
constructor(){
this.todos=[];
}
add(todo){
this.todo.push(todos);
}
remove(indexOfTodo){
if(indexOfTodo>=0 && indexOfTodo<this.todos.length){
this.todos.slice(indexOfTodo,1);
}
else{
console.error('Invalid index set for removing');
}
}

update(index,updatedTodo){
if(index>=0 && index<this.todos.length){
this.todos[index]=updatedTodo;
}
else{
console.error('invalid index to update');
}
}

getAll(){
return this.todos;
}

get(indexOfTodo){
if(indexOfTodo>=0 && indexOfTodo<this.todos.length){
return this.todos[indexOfTodo];
}else{
console.error('Invalid index provided for retrival');
return null;
}

}

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

const myTodoList = new Todo();

myTodoList.add('Task 1');
myTodoList.add('Task 2');
myTodoList.add('Task 3');

console.log('All Todos:', myTodoList.getAll());

myTodoList.remove(1);
console.log('After removing at index 1:', myTodoList.getAll());

myTodoList.update(0, 'Updated Task 1');
console.log('After updating at index 0:', myTodoList.getAll());

console.log('Task at index 0:', myTodoList.get(0));

myTodoList.clear();
console.log('After clearing all todos:', myTodoList.getAll());

module.exports = Todo;
18 changes: 18 additions & 0 deletions week-1/Week-1-assignment-with-tests/01-js/medium/palindrome.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,25 @@
*/

function isPalindrome(str) {
var left=0;
var right=str.length-1;
while(left<right){
if(str[right]!=str[left]){
return 0;
}
left++;
right--;
}
return true;
}

var string="Madam";
var sort=string.toLowerCase();
if(isPalindrome(sort)){
console.log("It is a palindrome");
}
else{
console.log("It is not a palindrome");
}

module.exports = isPalindrome;
13 changes: 11 additions & 2 deletions week-1/Week-1-assignment-with-tests/01-js/medium/times.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,14 @@ Hint - use Date class exposed in JS
*/

function calculateTime(n) {
return 0.01;
}
sum=0;
let start=Date.now();
for(var i=0;i<=n;i++){
sum+=i;
}
let timeTaken=Date.now()-start;
return timeTaken;
}

var answer=calculateTime(10000000000);
console.log(answer);
14 changes: 14 additions & 0 deletions week-1/Week-1-assignment-with-tests/02-async-js/easy/1-counter.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
// ## Create a counter in JavaScript

// We have already covered this in the second lesson, but as an easy recap try to code a counter in Javascript
// It should go up as time goes by in intervals of 1 second

var count=1;
const counter=()=>{
console.clear();
console.log(count);
count+=1;
}

setInterval(counter,1000);

This file was deleted.

85 changes: 85 additions & 0 deletions week-1/Week-1-assignment-with-tests/02-async-js/easy/2-counter.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
// <!-- ## Counter without setInterval

// Without using setInterval, try to code a counter in Javascript. There is a hint at the bottom of the file if you get stuck. -->

var count=1;

const counter=()=>{
console.clear();
console.log(count);
count+=1;
setTimeout(counter,1000);

}

counter();





































































// (Hint: setTimeout)
Loading