Skip to content

JavaScript Test 5

Asabeneh edited this page Apr 4, 2020 · 1 revision
  1. a) A weight of an object is different on different planet and it is calculated as w = mg. Write a function which calculate the weight of an object. It takes mass and gravity as a parameter. If the gravity is not given assume it is earth and the the approximate gravity of the earth is 10 m/s2. A weight of a 100 kg object on different planets is as follows:
  console.log(weight(100)) //Earth
1000 N 
  console.log(weight(100, 25)) //Jupiter
2500 N 
  console.log(weight(100, 0.58)) //Pluto
58 N
  console.log(weight(100, 1.62)) //Moon
162 N

b) Write a function which takes mass of an object and name of a planet and it returns the weight of the object in the planet. Different gravities in m/s2:

  • sun: 274
  • Jupiter:25
  • Earth: 9.79
  • Venus: 8.87
  • Mars:3.71
  • Moon: 1.62
  console.log(weightOfObject(100, 'earth'))
979.00 N
  console.log(weightOfObject(100, 'Earth'))
979.00 N
  console.log(weightOfObject(100, 'Venus'))
887.00 N
  console.log(weightOfObject(100, 'MOON'))
162 N
console.log(weightOfObject(100, 'moon'))
162 N
  1. Write a function called cleanText. The function takes raw text as a parameter and returns the clean text.
   const sentence = `%I $am@% a %tea@cher%, &and& I lo%#ve %tea@ching%;. There $is nothing; &as& mo@re rewarding as educa@ting &and& @emp%o@wering peo@ple. ;I found tea@ching m%o@re interesting tha@n any other %jo@bs. %Do@es thi%s mo@tivate yo@u to be a tea@cher!?`;
console.log(cleanText(sentence));
  `I am a teacher and I love teaching. There is nothing as more rewarding as educating and empowering people. I found teaching more interesting than any other jobs. Does this motivate you to be a teacher?`
  1. After cleaning the text in the sentence from question number 2 you will get the following text. Count the number of words in this text. Don't include words with only one letter.
 const sentence =  `I am a teacher and I love teaching. There is nothing as more rewarding as educating and empowering people. I found teaching more interesting than any other jobs. Does this motivate you to be a teacher?`
console.log(countWords(sentence));
31
  1. How many words were used to construct this sentence. Now, don't exclude one letter words.
 console.log(varietyOfWords(sentence))
28
  1. Create a function called countOnlyMainWords which count verbs which main importance in the sentence. The function return an object with keys words which is going to be array of main words and count, the number of main words.
 const supportWords = ['I', 'me', 'mine', 'myself', 'you', 'yours', 'yourself', 'he', 'him', 'his', 'himself', 'she', 'her', 'hers', 'herself', 'it', 'its', 'itself', 'we', 'us', 'our', 'ours', 'ouselves','they', 'them', 'themselves', 'be', 'been', 'being', 'am','is', 'was','are', 'were', 'do','does', 'did', 'done', 'has', 'have', 'had', 'will', 'would', 'can', 'could', 'may', 'might', 'shall', 'should', 'and', 'but', 'than', 'the', 'an','as', 'a', 'any', 'to', 'if', 'in', 'into','there', 'this', 'that', 'those', 'more', 'most', 'many', 'much', 'less', 'for', 'by', 'of', 'at', 'on','or','not','how','who', 'what', 'which', 'whose','when','why', 'where', 'so', 'also'];
console.log(countOnlyMainWords(sentence))
{
words:[
  "teacher",
 "love",
 "teaching",
 "nothing",
 "rewarding",
 "educating",
 "empowering",
 "people",
 "found",
 "teaching",
"interesting",
"other",
"jobs",
"motivate",
"teacher"
],
count:15
} 
  1. Write a function called headAndTail which find the head or tail of the array. Head is the first five elements in the array and similarly tail is the last five elements in the array. First, check the array has more than five elements if not return the array itself. And, also check if the parameter passed is an array if not return an appropriate feedback.
  const numbers = [1, 3, 6, 8, 11, 4, 5, 10, 9];
console.log(headAndTail(numbers).head)
[1, 3, 6, 8, 11]
console.log(headAndTail(numbers).tail)
[11, 4, 5, 10, 9]
  1. Write a function called stat, it takes only an array as a parameter and calculates mean, median, mode and range from students JavaScript test array.
const scores = [37, 34, 49, 57, 70.5, 61, 67.5, 90, 66.5, 70, 35.5, 44, 43, 29, 12.5, 95, 57, 36.5, 88, 33, 77.5, 82.5];
console.log(stat(scores).mean);
console.log(stat(scores).median);
console.log(stat(scores).range);
console.log(stat(scores).mode)
  1. Write two functions which are called arrayOfRgbColors and arrayOfHexaColors which take positive integer as parameter. a. arrayOfRgbColors:
  console.log(arrayOfRgbColors())
["rgb(128, 124, 101)"]
  console.log(arrayOfRgbColors(3))
["rgb(201, 137, 95)", "rgb(35, 67, 50)", "rgb(21, 217, 92)"]
  console.log(arrayOfRgbColors(5))
["rgb(201, 137, 95)", "rgb(35, 67, 50)", "rgb(21, 217, 92)", "rgb(176, 113, 68)", "rgb(245, 70, 80)"]

a. arrayOfHexaColors:

      console.log(arrayOfHexaColors())
        ["#402796"]
      console.log(arrayOfHexaColors(3))
        ["#54775c", "#d796db", "#83ae76"]
      console.log(arrayOfHexaColors(5))
        ["#a9db90", "#c97dd1", "#af276c", "#d72dcd", "#615313"]
  1. Write a function called arrayOfHexaOrRgb which generate an array of hexa or rgb colors:
          console.log(arrayOfHexaOrRgb())
          //["#53aa1a"]
          console.log(arrayOfHexaOrRgb('hexa'))
          //["#ebca77"]
          console.log(arrayOfHexaOrRgb('Hexa'));
          // ["#1f90e4"]
          console.log(arrayOfHexaOrRgb('Hexadecimal'))
          // ["#2731e6"]
          console.log(arrayOfHexaOrRgb('hexa', 5))
          //["#5f3e30", "#1008f8", "#5913e2", "#ab94dd", "#87e790"]
          console.log(arrayOfHexaOrRgb('rgb'))
          //["rgb(120, 81, 84)"]
          console.log(arrayOfHexaOrRgb('RGB'))
          //["rgb(138, 191, 84)"]
          console.log(arrayOfHexaOrRgb('rgb',3))
          //["rgb(214, 243, 237)", "rgb(214, 243, 237)", "rgb(214, 243, 237)"]
  1. On this data folder there are two speech transcripts. Use the supportWords array to exclude which are not that much relevance for the message of the speech.
  • a. Write a function which find the 10 most used words in the speech.
  • b. Find the Melina's and Michele's 10 most frequently used words in their speech
  • c. Do you think there is similarity between the speeches ?

Solution is available here

Clone this wiki locally