Untitled

mail@pastecode.io avatar
unknown
plain_text
2 months ago
5.6 kB
2
Indexable
Never
// ** you can use the commentout function execution
// to see the output **

// reduce method
// qn:-1

function UniqueString(strings) {
  // var obj={};
  // for(let i of strings){
  //   if(obj[i]) obj[i]++;
  //   else obj[i]=1;
  // }
  // return obj;
  return strings.reduce((prev,curr)=>{
    if(prev[curr]) prev[curr]++;
    else prev[curr]=1;
    return prev;
  },{})
  
}
// console.log(UniqueString(strings));

// qn :2

function ProductOfMultipleOfThreeOrFive(numbers1) {
  var sum=0;
  numbers1.forEach((items) => {
    if(items%3===0 || items%5===0){
      sum+=items;
    }
  });
  return sum;
}
// console.log(ProductOfMultipleOfThreeOrFive(numbers1));

// filter method Qn:1\

function palindromesString(words) {
  return words.filter((items)=>{
    return items===items.split("").reverse().join("");
  })
}
// console.log(palindromesString(words))

// filter method Qn:2

function findImage(filePaths) {
  return filePaths.filter((items)=>{
    var temp=items.split(".");
    if(temp[1]==="jpg" || temp[1]==="png") return true;
  })
}

// console.log(findImage(filePaths))

// map method Qn:1
function swapStrings(strings1) {
    let result = strings1.map((item) => {
      let first = item[0];
      let last = item[item.length - 1]; 
      let swapString = last + item.substring(1, item.length - 1) + first;
      return swapString;
    });
    return result;
  }
// console.log(swapStrings(strings1));

// foreach method Qn:1
function countWords(sentences) {
  return sentences.forEach((items,index)=>{
    let arr=items.split(" ");
    console.log(`sentence ${index+1} contains ${arr.length} words.`);
  })
}
// countWords(sentences)

// combination method Qn:1
function sumOfSquaresOfOddNumbers(numbers) {
  return numbers.filter((items)=>items%2!==0).map((items)=>items**2).reduce((prev,curr)=>prev+curr,0);
}
// console.log(sumOfSquaresOfOddNumbers(numbers));

// combination method Qn:2
let typeOfCourse = [
  { id: 1, name: "Developer" },
  { id: 2, name: "Tester" },
];
let CourseDurationDirectory = {
  1: `6 month`,
  2: `1 year`,
  3: `2 year`,
};
let Category = {
  3: "Fullstack",
  4: "manual tester",
  5: "automation tester",
};
function massageArray(exampleInputArray) {
  return exampleInputArray.map((course) => {
    const { courseName, courseDuration, Category: categoryId, type, techTools, techdetails } = course;
    
    const filteredTechTools = Object.entries(techTools)
      .filter(([key, value]) => value !== null && value !== '')
      .map(([key, value]) => ({ language: value, details: techdetails[key] }));

    return {
      courseName,
      courseDuration: CourseDurationDirectory[courseDuration],
      Category: Category[categoryId],
      type: typeOfCourse.find((c) => c.id === type).name,
      techTools: filteredTechTools,
    };
  });
}
// console.log(massageArray(exampleInputArray));


// Implementation methods ps Qn:6

function ImplementationPS6(students) {
  let subjectsHash = {
    1: "Javascript",
    2: "HTML",
    3: "CSS",
    4: "Java",
    5: "Rust",
  };
  let obj={};
 students.forEach((student)=>{
    let name=student.name;
    let subjectID=student.subjectID;
    let subject=subjectsHash[subjectID];
    if(!obj[name]){
      obj[name]=[];
    }
    obj[name].push(subject);
  })
  return obj;
}
// console.log(ImplementationPS6(students));

// Implementation methods ps Qn:7

function ImplementationPS7(allStudentsMarksData) {
  let arr1=[];
  const arr=allStudentsMarksData.forEach((student)=>{
    let obj={
      name:student.name,
      marks:[]
    }

    for(let i=1;i<=5;i++){
      var subject=`subject${i}`;
      var score=`marks${i}`;
      if(student[subject] && student[score]){
        obj.marks.push({
          subject: student[subject],
          score: student[score],
        });
      }
    }
    arr1.push(obj);
  });
  return arr1;
}
// console.log(ImplementationPS7(allStudentsMarksData));


// Implementation methods ps Qn:9

function ImplementationPS9(voters) {
  let obj = {
    "numYoungVotes": 0,
    "numYoungPeople": 0,
    "numMidVotesPeople": 0,
    "numMidsPeople": 0,
    "numOldVotesPeople": 0,
    "numOldsPeople": 0,
  };
  voters.forEach((voter)=>{
    if(voter.age>=18 && voter.age<=20){
      obj["numYoungPeople"]++;
      if(voter.voted===true){
        obj["numYoungVotes"]++;
      }
    }else if(voter.age>=21 && voter.age<=50){
      obj["numMidsPeople"]++;
      if(voter.voted===true){
        obj["numMidVotesPeople"]++;
      }
    }else{
      obj["numOldsPeople"]++;
      if(voter.voted===true){
        obj["numOldVotesPeople"]++;
      }
    }
  });
  return obj;
}
// console.log(ImplementationPS9(voters));

// Implementation methods ps Qn:10
function ImplementationPS10(data) {
  let obj={};
  data.forEach((favoriteIceCream)=>{
    for(let i of favoriteIceCream.favoriteIceCreams){
      if(obj[i]){
        obj[i]++;
      }else{
        obj[i]=1;
      }
    }
  });
  return obj;
}
// console.log(ImplementationPS10(data));

// Dont remove below export statement part

export {
  // reruce
  UniqueString,
  ProductOfMultipleOfThreeOrFive,
  // filter
  palindromesString,
  findImage,
  // map
  swapStrings,
  // forEach
  countWords,
  // combination
  sumOfSquaresOfOddNumbers,
  massageArray,
  // Implementation methods problem statement
  ImplementationPS6,
  ImplementationPS7,
  ImplementationPS9,
  ImplementationPS10,
};
Leave a Comment