Skip to content

JavaScript Test 4

Asabeneh edited this page Jun 22, 2019 · 1 revision

Read each questions carefully. After you finish send to my email as .html or .txt file.

Don't use map, filter, reduce and other functional programming methods in this test.
  1. 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}
  1. Create a function called isPrime which check if a number is prime or not.
      console.log(isPrime(0)); // false
      console.log(isPrime(1)); // false
      console.log(isPrime(2)); // true
      console.log(isPrime(3)); // true
      console.log(isPrime(5));// true
  1. Write a function rangeOfPrimes. 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}
  1. Create a function called isEmpty which check if the parameter is empty. If the parameter is empty, it returns true else it returns false.
    isEmpty('') // true
    isEmpty(' ') // true
    isEmpty('Asabeneh') // false
    isEmpty([]) // true
    isEmpty(['HTML', 'CSS', 'JS']) // false;
    isEmpty({}) //true
    isEmpty({name:'Asabeneh', age:200}) // false
  1. a. Create a function called reverse which take a parameter and it returns the reverse of the parameter. Don't use the built in reverse method.
   reverse('cat'); // tac
   reverse('123'); // 321

b. Create a function called isPalindrome which check if a parameter is a palindrome or not. Use the function from a to reverse words.

      console.log(isPalindrome('Anna'));//true
      console.log(isPalindrome(121));//true
      console.log(isPalindrome('Noon'));//true
      console.log(isPalindrome('Asa '));//true
      console.log(isPalindrome('Asab'));//false
      console.log(isPalindrome('cat'));//false
  1. Create a function called countPalindrowWords which counts the number of palindrome words in the palindoromeWords array or in any array.
      const words = [
        'Anna',
        'Asa',
        'Civic',
        'common',
        'Kayak',
        'Level',
        'Madam',
        'Mom',
        'Noon ',
        'Rotor',
        'Sagas ',
        'Solar',
        'Stats',
        'Tenet ',
        'Wow'
      ];
  1. Count the number of palindrome words in the following sentence.
   const sentence = `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

    const users = [
    {
        _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
    }
    ];

    const products = [
  {
    _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']
  }
];
  1. 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.

    b. Create a function called signIn which allows user to sign in to the application

  2. The products array has three elements and each of them has six properties.

    a. Create a function called rateProduct which rates the product

    b. Create a function called averageRating which calculate the average rating of a product

  3. Create a function called likeProduct. This function will helps to like to the product if it is not liked and remove like if it was liked.