Skip to content

JavaScript Test 2020:Test 1

Asabeneh edited this page Apr 8, 2020 · 13 revisions

JavaScript Test

Question 1

  1. Print the following pattern using loop and console.log(). (2.5 pts)

    #
    ##
    ###
    ####
    #####
    ######
    #######
  2. Print the following pattern using loop and console.log(). (2.5 pts)

    0 x 0 = 0
    1 x 1 = 1
    2 x 2 = 4
    3 x 3 = 9
    4 x 4 = 16
    5 x 5 = 25
    6 x 6 = 36
    7 x 7 = 49
    8 x 8 = 64
    9 x 9 = 81
    10 x 10 = 100
  3. Print the following pattern using loop and console.log(). (2.5 pts)

    i    i^2   i^3
    0    0     0
    1    1     1
    2    4     8
    3    9     27
    4    16    64
    5    25    125
    6    36    216
    7    49    343
    8    64    512
    9    81    729
    10   100   1000
  4. Using the countries array create the following array of arrays. The country name, the first three letters of the country name and the length of the country name.(2.5 pts)

const countries = [
  'ALBANIA',
  'BOLIVIA',
  'CANADA',
  'DENMARK',
  'ETHIOPIA',
  'FINLAND',
  'GERMANY',
  'HUNGARY',
  'IRELAND',
  'JAPAN',
  'KENYA'
]
createArrayOfArrays(countries)
[
  ['Albania', 'ALB', 7],
  ['Bolivia', 'BOL', 7],
  ['Canada', 'CAN', 6],
  ['Denmark', 'DEN', 7],
  ['Ethiopia', 'ETH', 8],
  ['Finland', 'FIN', 7],
  ['Germany', 'GER', 7],
  ['Hungary', 'HUN', 7],
  ['Ireland', 'IRE', 7],
  ['Japan', 'JAP', 5],
  ['Kenya', 'KEN', 5]
]

Question 2

const products = [
  { product: 'banana', price: 3 },
  { product: 'mango', price: 6 },
  { product: 'potato', price: ' ' },
  { product: 'avocado', price: 8 },
  { product: 'coffee', price: 10 },
  { product: 'tea', price: '' }
]
  1. Print the product items as follows: (2.5 pts)

    The price of banana is 3 euros.
    The price of mango is 6 euros.
    The price of potato is unknown.
    The price of avocado is 8 euros.
    The price of coffee is 10 euros.
    The price of tea is unknown.
  2. Calculate the sum of all the prices using for, for of, or forEach (2.5 pts)

  3. Use method chaining to get the sum of the prices(map, filter, reduce) (2.5 pts)

  4. Calculate the sum of all the prices using reduce only (2.5 pts)

Question 3

  1. Write a program which tells the number of days in a month.(5 pts)

      Enter month: January
      January has 31 days.
    
      Enter month: JANUARY
      January has 31 day
    
      Enter month: February
      February has 28 days.
    
      Enter month: FEbruary
      February has 28 days.
  2. Write a function which generate random id or hexadecimal color or rgb color (5 pts)

    console.log(generate())              //  vaH10t
    console.log(generate('id'))          // X3ZBWP
    console.log(generate('hexa'))        // '#ee33df'
    console.log(generate('hexadecimal')) // '#5ccdfa'
    console.log(generate('rgb'))         //  rgb(240, 180, 80)
    console.log(generate('RGB'))         // /rgb(240, 50, 80)
    

Question 4

  1. In the countries array remove the middle three countries and replace it with ESTONIA, FRANCE and GHANA.(2 pts)

    const countries = [
      'ALBANIA',
      'BOLIVIA',
      'CANADA',
      'DENMARK',
      'ETHIOPIA',
      'FINLAND',
      'GERMANY',
      'HUNGARY',
      'IRELAND',
      'JAPAN',
      'KENYA'
    ]
  2. Write a function which check if items of an array are unique? (4 pts)

      const arrOne = [1, 4, 6, 2, 1];
      console.log(checkUniqueness(arrOne)) // false
      const arrTwo = [1, 4, 6, 2, 3]
      console.log(checkUniqueness(arrTwo)) // true
  3. Write a function called checkDataTypes it takes an array and a data type. It returns true if the array has the same data types. (4 pts)

    const numbers = [1, 3, 4]
    const names = ['Asab', '30DaysOfJavaScript']
    const bools = [true, false, true, true, false]
    const mixedData = ['Asab', 'JS', true, 2019, { name: 'Asab', lang: 'JS' }]
    const object = [{ name: 'Asab', lang: 'JS' }]
    console.log(checkDataTypes(numbers, 'number')) // true
    console.log(checkDataTypes(numbers, 'string')) // false
    console.log(checkDataTypes(names, 'string')) // true
    console.log(checkDataTypes(bool, 'boolean')) // true
    console.log(checkDataTypes(mixedData, 'boolean')) // false
    console.log(checkDataTypes(obj, 'object')) // true

Question 5

  1. Destructure the following array into frontEnd and backEnd:(1 pt)

    const fullStack = [
      ['HTML', 'CSS', 'JS', 'React'],
      ['Node', 'Express', 'MongoDB']
    ]
    console.log(frontEnd)  // ['HTML', 'CSS', 'JS', 'React']
    console.log(backEnd)   // ['Node', 'Express', 'MongoDB']
  2. A junior developer structure student name, skills and score in array of arrays which may not easy to read and to work with. Destructure the following array name to name, skills array to each skills, scores array to each scores in one line.(3 pts)

    const student = ['David', ['HTM', 'CSS', 'JS', 'React'], [98, 85, 90, 95]]
    console.log(name, html, css, js, react, htmlScore, cssScore, jsScore, reactScore)
  3. Write a function called convertArrayToObject which can convert the array to a structured object. (6 pts)

        const students = [
            ['David', ['HTM', 'CSS', 'JS', 'React'], [98, 85, 90, 95]],
            ['John', ['HTM', 'CSS', 'JS', 'React'], [85, 80, 85, 80]]
          ]
    
        console.log(convertArrayToObject(students))
        [
          {
            name: 'David',
            skills: ['HTM','CSS','JS','React'],
            scores: [98,85,90,95]
          },
          {
            name: 'John',
            skills: ['HTM','CSS','JS','React'],
            scores: [85, 80,85,80]
          }
        ]

Question 6

  1. Write a function sumOfAllNums which takes unlimited number of arguments both in function declaration and arrow functions (2.5 pts)

    sumOfAllNums(2, 3, 1)  // 6
    sumOfAllNums(1, 2, 3, 4, 5) // 15
    sumOfAllNums(1000, 900, 120) // 2020
  2. An array is written as follows (2.5 pts)

    const [x, y] = [2, (a) => a ** 2 - 4 * a + 3 ]

    Find the following:

    • Find x
    • Find y
    • Find y(x)
  3. Copy the student object to newStudent without mutating the original object. In the new object add the following ? (5 pts)

  • Add Bootstrap with level 8 to the front end skill sets

  • Add Express with level 9 to the back end skill sets

  • Add SQL with level 8 to the data base skill sets

  • Add SQL without level to the data science skill sets

  • Add devOps a new skill set and add AWS with level 7, Jenkin with level 8, Git with level 8

        const student = {
          name: 'David',
          age: 25,
          skills: {
            frontEnd: [
              { skill: 'HTML', level: 10 },
              { skill: 'CSS', level: 8 },
              { skill: 'JS', level: 8 },
              { skill: 'React', level: 9 }
            ],
            backEnd: [
              { skill: 'Node',level: 7 },
              { skill: 'GraphQL', level: 8 },
            ],
            dataBase:[
              { skill: 'MongoDB', level: 7.5 },
            ],
            dataScience:['Python', 'R', 'D3.js']
          }
        }

The copied object output should look like this:

    {
    name: 'David',
    age: 25,
    skills: {
      frontEnd: [
        {skill: 'HTML',level: 10},
        {skill: 'CSS',level: 8},
        {skill: 'JS',level: 8},
        {skill: 'React',level: 9},
        {skill: 'BootStrap',level: 8}
      ],
      backEnd: [
        {skill: 'Node',level: 7},
        {skill: 'GraphQL',level: 8},
        {skill: 'Express',level: 9}
      ],
      dataBase: [
        { skill: 'MongoDB',level: 7.5},
        { skill: 'SQL',level: 8}
      ],
      dataScience: ['Python','R','D3.js','SQL'],
      devOps:[
          { skill: 'AWS', level: 7 },
          { skill: 'Jenkin', level: 7 },
          { skill: 'Git', level: 8 }
        ]
    }
  }

Question 7

  1. Time is a very important physical quantity for web development. Develop a small program which print different time formats. Do not use any library, but you can use the Date constructor. (5 pts)

    console.log(showDateTime())                      // 4/4/2020
    console.log(showDateTime('dd/mm/yyyy'))          // 4/4/2020
    console.log(showDateTime('dd-mm-yyyy'))          // 4-4-2020
    console.log(showDateTime('dd-mm-yyyy hh:mm'))    // 4-4-2020 16:21
    console.log(showDateTime('dd/mm/yyyy hh:mm'))    // 4/4/2020 16:21
    console.log(showDateTime('MMMM DD, YYYY'))       // April 4, 2020
    console.log(showDateTime('MMMM DD, YYYY hh:mm')) // April 4, 2020 16:21
    console.log(showDateTime('MMM DD, YYYY hh:mm'))  // Apr 4, 2020 16:21
  2. The following todoList has three tasks. Create an addTask, removeTask, editTask, toggleTask, toggleAll, removeAll functions to modify the todoList. Use the showDateTime function from the above question. (pt 5)

    const todoList = [
      {
        task:'Prepare JS Test',
        time:'5/4/2020 8:30',
        completed:true
      },
      {
        task:'Give JS Test',
        time:'6/4/2020 10:00',
        completed:false
      },
      {
        task:'Assess Test Result',
        time:'4/3/2019 1:00',
        completed:false
      }
    ]

Question 8

  1. Use the countries API to fetch data about countries. (5 pt)

    const API_URL = 'https://restcountries.eu/rest/v2/all'

    Use the countries API to fetch data about countries.

    • How many languages are there in the countries API
    • Find the 15 most spoken languages
    [
    {language: "English", countries: 91}
    {language: "French", countries: 45}
    {language: "Arabic", countries: 25}
    {language: "Spanish", countries: 24}
    {language: "Portuguese", countries: 9}
    {language: "Russian", countries: 9}
    {language: "Dutch", countries: 8}
    {language: "German", countries: 7}
    {language: "Chinese", countries: 5}
    {language: "Serbian", countries: 4}
    {language: "Swahili", countries: 4}
    {language: "Italian", countries: 4}
    {language: "Swedish", countries: 3}
    {language: "Albanian", countries: 3}
    {language: "Croatian", countries: 3}
    ]
    • Find the 10 most largest countries
    [
    {country: "Russian Federation", area: 17124442}
    {country: "Antarctica", area: 14000000}
    {country: "Canada", area: 9984670}
    {country: "China", area: 9640011}
    {country: "United States of America", area: 9629091}
    {country: "Brazil", area: 8515767}
    {country: "Australia", area: 7692024}
    {country: "India", area: 3287590}
    {country: "Argentina", area: 2780400}
    {country: "Kazakhstan", area: 2724900}
    ]
  2. Make a function called add which give truthy value when add(a, b) === add(a)(b) (5 pt)

    console.log(add(2,3))                //5
    console.log(add(2)(3))               //5
    console.log(add(2,3) === add(2)(3))  // true

Question 9

  1. What is the difference between forEach, map, filter and reduce? (1 pt)
  2. What is the difference between find and filter? (1pt)
  3. Which of the following mutate array: map, filter, reduce, slice, splice, concat, sort, some? (2pt)
  4. Generate random hexadecimal and rgb colors (6 pts)
// the function should be valid for hexadecimal, hexaDecimal, HEXA, hexa, rgb or RGB

console.log(generateColor())          // #d1b0f0
console.log(generateColor('hexa'))    // #19da38

console.log(generateColor('rgb'))     // rgb(39,160,96)
console.log(generateColor('hexa', 3)) // [ '#ce5438', '#365696', '#20cd3e' ]
console.log(generateColor('rgb', 3))  // [ 'rgb(221,5,22)', 'rgb(119,169,192)', 'rgb(228,193,219)' ]

Question 10

  1. Create a function called mostFrequentWord which find the most frequent word in a sentence or a paragraph. (2.5 pts)

  2. Clean the following text and count the number of words in the variable sentence after cleaning.(2.5 pts)

    const sentence = `@He@ @%met%^$##%# his mom at no@on and s@he was watching %^$#an epso@ide%^$# of the begni@nging of her Sol@os. Her te@net%^$# hel@ped %^$#her to le@vel up her stats. %^$#After that h@e went to %^$#kayak driving Civic %^$#Honda.`
  3. Write a function which check palindrome words (2.5 pts)

  4. Count the number of palindrome words in the string sentence after cleaning.(2.5 pts)