- Notifications
You must be signed in to change notification settings - Fork305
JavaScript Test 2
Asabeneh edited this pageMay 13, 2019 ·10 revisions
- Write a function which count the number of occurrence of words in a paragraph or a sentence.The function countWords takes a paragraph and two words as parameters. It compare which word is most frequently occurred in the paragraph.
constparagraph='I love teaching. If you do not love teaching what else can you love. I love JavaScript if you do not love something which can give life to your application what else can you love.';console.log(countWords(paragraph,'love','you'));Thewordlovemorefrequentlyoccurredthanyou.
- Write a function which takes an array parameter and returns an array of the data types of each item:
constarr=['Asabeneh',100,true,null,undefined,{job:'teaching'}];console.log(checkDatatTypes(arr));["string","number","boolean","object","undefined","object"]constmixedData=["John",25,"David",30,"Sara",22];console.log(checkDatatTypes(mixedData));["string","number","string","number","string","number"];
- Create a function which filter ages greater than 18.
constages=[35,30,17,18,15,22,16,20];console.log(agesGreaterEighteen(ages));[35,30,22,,20];
- Write a function which calculate the average age of the group.
console.log(averageAge(ages));22
- Write a function named shuffle, it takes an array parameter and it returns a shuffled array.
shuffle([1,2,3,4,5];[3,1,5,2,4]
- Write a function which can generate a random Finnish car code.
console.log(genCarPlateNum())AFG-205console.log(genCarPlateNum())JCB-586
- The following shopping cart has four products. Create an addProduct, removeProduct ,editProduct , removeAll functions to modify the shopping cart.
constshoppingCart=['Milk','Coffee','Tea','Honey'];addProduct("Meat");["Milk","Coffee","Tea","Honey","Meat"]editProduct(3,"Sugar");["Milk","Coffee","Tea","Sugar","Meat"]removeProduct(0);["Coffee","Tea","Sugar","Meat"]removeProduct(3);["Coffee","Tea","Sugar"]
- The following todoList has three tasks. Create an addTask, removeTask, editTask, toggleTask, toggleAll, removeAll functions to modify the todoList.
consttodoList=[{task:'Prepare JS Test',time:'4/3/2019 8:30',completed:true},{task:'Give JS Test',time:'4/3/2019 10:00',completed:false},{task:'Assess Test Result',time:'4/3/2019 1:00',completed:false}]
- Write a function which check if items of an array are unique?
const arrOne = [1, 4, 6, 2, 1];console.log(checkUniqueness(arrOne));falseconst arrTwo = [1, 4, 6, 2, 3]console.log(checkUniqueness(arrTwo));true
- Write a function which filter users who has scoresGreaterThan85,Write a function which addUser to the user array only if the user does not exist.Write a function which addUserSkill which can add skill to a user only if the user exist.Write a function which editUser if the user exist in the users array.
constusers=[{name:'Brook',scores:75,skills:['HTM','CSS','JS'],age:16},{name:'Alex',scores:80,skills:['HTM','CSS','JS'],age:18},{name:'David',scores:75,skills:['HTM','CSS'],age:22},{name:'John',scores:85,skills:['HTM'],age:25},{name:'Sara',scores:95,skills:['HTM','CSS','JS'],age:26},{name:'Martha',scores:80,skills:['HTM','CSS','JS'],age:18},{name:'Thomas',scores:90,skills:['HTM','CSS','JS'],age:20}];