From 6fd93adf685ef9bf1f63517b417e8b9acccba441 Mon Sep 17 00:00:00 2001 From: Fitsumhelina Date: Fri, 22 Nov 2024 01:08:56 +0300 Subject: [PATCH] second option added --- Exercises/day-1/object-exercise-3.js | 33 +++++++++++++++++++--------- 1 file changed, 23 insertions(+), 10 deletions(-) diff --git a/Exercises/day-1/object-exercise-3.js b/Exercises/day-1/object-exercise-3.js index 515414d..c66c54b 100644 --- a/Exercises/day-1/object-exercise-3.js +++ b/Exercises/day-1/object-exercise-3.js @@ -103,7 +103,7 @@ const products = [ // Imagine you are getting the above users collection from a MongoDB database. // a. Create a function called signUp which allows user to add to the collection. If user exists, inform the user that he has already an account. const newuser = { - _id: 'abc123', + _id: 'eedfcf', username: 'John Doe', email: 'johndoe@johndoe.com', password: '123456', @@ -111,18 +111,31 @@ const products = [ isLoggedIn: false, }; const signUp = (newuser) => { - const userexist = users.find((user) => user._id === newuser._id) - if (userexist) { - console.log('User already exists!') - return; - } - else{ - users.push(newuser) - console.log('User added successfully!') + // const userExists = users.some((user) => user._id === newuser._id); + // if (userExists) { + // console.log('User already exists!') + // return; + // } + // else{ + // users.push(newuser) + // console.log('User added successfully!') + // } + + for (const user of users ){ + if(user._id === newuser._id){ + console.log('User already exists!') + return; + } + else{ + users.push(newuser) + console.log('User added successfully!') + break; + } } } signUp(newuser); - + + // b. Create a function called signIn which allows user to sign in to the application // The products array has three elements and each of them has six properties.