- Notifications
You must be signed in to change notification settings - Fork305
JavaScript Test 4
- Create a function which solve quadratic equation ax2 + bx + c = 0. A quadratic equation may have one, two or no solution. The function should return a set of the solution(s).
console.log(solveQuadratic());//Set(1) {0}console.log(solveQuadratic(1,4,4));//Set(1) {-2}console.log(solveQuadratic(1,-1,-2));//{2, -1}console.log(solveQuadratic(1,7,12));//Set(2) {-3, -4}console.log(solveQuadratic(1,0,-4));//{2, -2}console.log(solveQuadratic(1,-1,0));//{1, 0}
- Create a function calledisPrime which check if a number is prime or not.
console.log(isPrime(0));// falseconsole.log(isPrime(1));// falseconsole.log(isPrime(2));// trueconsole.log(isPrime(3));// trueconsole.log(isPrime(5));// true
- Write a functionrangeOfPrimes. It takes two parameters, a starting number and an ending number . The function returns an object with properties primes and count. The primes value is an array of prime numbers and count value is the number of prime numbers in the array. See example
console.log(rangeOfPrimes(2,11));//{primes:[2, 3, 5, 7, 11], count:5}console.log(rangeOfPrimes(50,60));//{primes:[53, 59], count:2}console.log(rangeOfPrimes(95,107));//{primes:[97, 101, 103, 107], count:4}
- Create a function calledisEmpty which check if the parameter is empty. If the parameter is empty, it returns true else it returns false.
isEmpty('')// trueisEmpty(' ')// trueisEmpty('Asabeneh')// falseisEmpty([])// trueisEmpty(['HTML','CSS','JS'])// false;isEmpty({})//trueisEmpty({name:'Asabeneh',age:200})// false
- a. Create a function calledreverse which take a parameter and it returns the reverse of the parameter. Don't use the built in reverse method.
reverse('cat');// tacreverse('123');// 321
b. Create a function calledisPalindrome which check if a parameter is a palindrome or not.Use the function from a to reverse words.
console.log(isPalindrome('Anna'));//trueconsole.log(isPalindrome(121));//trueconsole.log(isPalindrome('Noon'));//trueconsole.log(isPalindrome('Asa '));//trueconsole.log(isPalindrome('Asab'));//falseconsole.log(isPalindrome('cat'));//false
- Create a function calledcountPalindrowWords which counts the number of palindrome words in the palindoromeWords array or in any array.
constwords=['Anna','Asa','Civic','common','Kayak','Level','Madam','Mom','Noon ','Rotor','Sagas ','Solar','Stats','Tenet ','Wow'];
- Count the number of palindrome words in the following sentence.
constsentence=`He met his mom at noon and she was watching an epsoide of the begninging of her Solos. Her tenet helped her to level up her stats. After that he went to kayak driving Civic Honda.`
Questions:8, 9 and 10 are based on the following two arrays:users and products
constusers=[{_id:'ab12ex',username:'Alex',email:'alex@alex.com',password:'123123',createdAt:'17/05/2019 9:00 AM',isLoggedIn:false},{_id:'fg12cy',username:'Asab',email:'asab@asab.com',password:'123456',createdAt:'17/05/2019 9:30 AM',isLoggedIn:true},{_id:'zwf8md',username:'Brook',email:'brook@brook.com',password:'123111',createdAt:'17/05/2019 9:45 AM',isLoggedIn:true},{_id:'eefamr',username:'Martha',email:'martha@martha.com',password:'123222',createdAt:'17/05/2019 9:50 AM',isLoggedIn:false},{_id:'ghderc',username:'Thomas',email:'thomas@thomas.com',password:'123333',createdAt:'17/05/2019 10:00 AM',isLoggedIn:false}];constproducts=[{_id:'eedfcf',name:'mobile phone',description:'Huawei Honor',price:200,ratings:[{userId:'fg12cy',rate:5},{userId:'zwf8md',rate:4.5}],likes:[]},{_id:'aegfal',name:'Laptop',description:'MacPro: System Darwin',price:2500,ratings:[],likes:['fg12cy']},{_id:'hedfcg',name:'TV',description:'Smart TV:Procaster',price:400,ratings:[{userId:'fg12cy',rate:5}],likes:['fg12cy']}];
Imagine you are getting the above users collection from a MongoDB database.
a. Create a function calledsignUp which allows user to add to the collection. If user exists, inform the user that he has already an account.
b. Create a function calledsignIn which allows user to sign in to the application
The products array has three elements and each of them has six properties.
a. Create a function calledrateProduct which rates the product
b. Create a function calledaverageRating which calculate the average rating of a product
Create a function calledlikeProduct. This function will helps to like to the product if it is not liked and remove like if it was liked.