diff --git a/assignments/bmi_calculator/bmi_calculator.js b/assignments/bmi_calculator/bmi_calculator.js index 0cdd351..ffeaa49 100644 --- a/assignments/bmi_calculator/bmi_calculator.js +++ b/assignments/bmi_calculator/bmi_calculator.js @@ -1,6 +1,13 @@ // This function should return the BMI for a person function BMICalculator(mass, height) { // Write your code here + if(mass>0 && height>0){ + BMI = mass / (height * height) + return BMI + } + else{ + return "INVALID INPUT"; + } } module.exports = BMICalculator; diff --git a/assignments/can_drive/can_drive.js b/assignments/can_drive/can_drive.js index f2f1251..9d56844 100644 --- a/assignments/can_drive/can_drive.js +++ b/assignments/can_drive/can_drive.js @@ -1,5 +1,16 @@ function CanDrive(hasDrivingLiscence, isTired, isSober) { // Write you code here + if(hasDrivingLiscence===false){ + return "You cannot drive"; + } + else if(hasDrivingLiscence===true && (isTired===true || isSober===false)){ + return "You shouldn't drive"; + } + else if(hasDrivingLiscence===true && (isTired===false || isSober===true)){ + return "You can drive"; + } } module.exports = CanDrive; + + diff --git a/assignments/create_object/create_object.js b/assignments/create_object/create_object.js index 775bc8c..09e0a33 100644 --- a/assignments/create_object/create_object.js +++ b/assignments/create_object/create_object.js @@ -1,5 +1,12 @@ function CreateObject(arr) { // Write your code here + obj={}; + arr.forEach((element,index,arr)=>{ + if(index%2===0){ + obj[element]=arr[index+1]; + } + }); + return obj; } module.exports = CreateObject; diff --git a/assignments/favourite_movie/favourite_movie.js b/assignments/favourite_movie/favourite_movie.js index a8148eb..cbc089f 100644 --- a/assignments/favourite_movie/favourite_movie.js +++ b/assignments/favourite_movie/favourite_movie.js @@ -2,6 +2,12 @@ const movies = []; function favouriteMovie(operation, movie) { // Write your code here + if(operation==="add"){ + movies.push(movie); + } + else if(operation==="remove"){ + movies.pop(); + } + return movies; } - module.exports = favouriteMovie; diff --git a/assignments/play_with_array/play_with_array.js b/assignments/play_with_array/play_with_array.js index 0601c6f..e30cbc2 100644 --- a/assignments/play_with_array/play_with_array.js +++ b/assignments/play_with_array/play_with_array.js @@ -7,7 +7,8 @@ function getEven(arr) { Write you code below */ - + var res=arr.filter(number=>number%2===0); + return res } function multiplyByN(arr, n) { @@ -18,6 +19,8 @@ function multiplyByN(arr, n) { Output: [3,9,13,165] Write you code below */ + var mul=arr.map(number=>number*n) + return mul } function removeNthElement(arr, n) { @@ -28,6 +31,8 @@ function removeNthElement(arr, n) { Output: [1,3,4,7] Write you code below */ + arr.splice(n,1); + return arr } module.exports = { diff --git a/assignments/run_callback/run_callback.js b/assignments/run_callback/run_callback.js index 04bf43e..6f866b4 100644 --- a/assignments/run_callback/run_callback.js +++ b/assignments/run_callback/run_callback.js @@ -1,5 +1,7 @@ function RunCallback(a, b, cb) { // Write you code here, you need to add a and b, pass the result into callback function cb as argument return the result + var sum=a+b; + return cb(sum); } module.exports = RunCallback; diff --git a/assignments/todo_list/script.js b/assignments/todo_list/script.js index e69de29..a253b43 100644 --- a/assignments/todo_list/script.js +++ b/assignments/todo_list/script.js @@ -0,0 +1,17 @@ +var todoButton = document.querySelector('button'); +var userInput= document.querySelector('input'); +var todoList = document.querySelector('.todos') +var count = 0; +todoButton.addEventListener("click", AddTodo) +function AddTodo(){ + var todo=document.createElement('p'); + todo.setAttribute('key',count); + count=count+1; + todo.innerHTML=userInput.value; + todoList.appendChild(todo); + userInput.value=''; + todo.addEventListener('click',function(e){ + todoList.removeChild(todo); + }) +} + diff --git a/assignments/todo_list/script1.js b/assignments/todo_list/script1.js new file mode 100644 index 0000000..31b6dfd --- /dev/null +++ b/assignments/todo_list/script1.js @@ -0,0 +1,37 @@ +var todoButton = document.querySelector('button'); +var inputfield = document.querySelector('input'); +var todoList= document.querySelector('div.todos') +var todos=[] + +async function callAPI(){ + try{ + const response=await fetch('https://jsonplaceholder.typicode.com/todos') + .then(response=>response.json()) + .then(json=>{ + // const json=response.json() + todos=json.slice(0,10) + console.log(todos) + todos.forEach((todo,index)=>{ + const newTodo =document.createElement('p') + newTodo.setAttribute('key',index) + newTodo.innerHTML=todo.title + todoList.appendChild(newTodo) + }) + }) + .catch((e)=>console.log(e)) + } + catch(e){ + console.log(e); + } +} +callAPI() + +todoButton.addEventListener('click',function(){ + todoList.innerHTML='' + todos.filter(todo => todo.completed).forEach((todo,index)=>{ + const newTodo=document.createElement('p') + newTodo.setAttribute('key',index) + newTodo.innerHTML=todo.title + todoList.appendChild(newTodo) ; + }) +}) \ No newline at end of file diff --git a/assignments/todo_list/todo.html b/assignments/todo_list/todo.html index 8f6fac5..12fcf38 100644 --- a/assignments/todo_list/todo.html +++ b/assignments/todo_list/todo.html @@ -1,13 +1,25 @@