Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

67
Visualizações
Practicing JavaSccript

I currently practice JS and here is a question I'm struggling with solving this question. Could you try to solve this question and share your code?

An object like this: The composition of the object and the image of the screen. An object's value is one of the following nine patterns.

  • 'A+', 'A', 'B+', 'B', 'C+', 'C', 'D+', 'D', 'F'
{
 'classA': 'C',
 'classB': 'B',
 'classC': 'B+',
 'classD': 'D+',
 'classE': 'C+',
 'classF': 'A+',
}

The argument requiredClasses is an array of strings: ['classsE', 'classD', 'classO', 'classP']

Implement the function to return an object that satisfies the following conditions.

The keys held by the scores object must be included in the new object. However, the values must be converted into numbers and assigned according to the following principle.

A+ => 4.5

A => 4

B+ => 3.5

B => 3

C+ => 2.5

C => 2

D+ => 1.5

D => 1

F => 0

If there is an item that exists as an element of the requiredClasses array but does not exist as a key for scores, then that element becomes the key of the new object and must have a value of 0. If the object and array described in the example above are input as arguments, the object and the following are returned. The order between the elements may be different, but it is irrelevant to scoring.

I tried to solve this question by myself.

const getExamResult = (scores, requiredClasses) => {
  let newScores = {};
  const entries = Object.entries(scores);
  //객체 key,object
  let scoresKey = Object.keys(scores);
  const scoresValue = Object.values(scores);
  const newKeys = entries.map(el => el[0]);
  console.log(newKeys);
  const newValues = entries.map(el => { if (newKeys.key[0] === "A+"){
    return 4.5;
  }
  else if (newKeys.key === "A") {
    return 4;
  }
  else if (newKeys.key === "B+") {
    return 3.5;
  }
  else if (newKeys.key === "B") {
    return 3;
  }
  else if (newKeys.key === "C+") {
    return 2.5;
  }
  else if (newKeys.key === "C") {
    return 2;
  }
  else if (newKeys.key === "D+") {
    return 1.5;
  }
  else if (newKeys.key === "D") {
    return 1;
  }
  else 
    return 0;
  });

  for(let index in newKeys) {
    newScores[newKeys[index]] = newValues[index];
  }
  for (let index in requiredClasses) {
    if (newKeys.indexOf(`${requiredClasses[index]}`) === -1) {
      newScores.requiredClasses[index] = 0;
    }
  }
  return newScores;
}

Please let me know if you have better code or let me know which part is wrong. I really appreciate your answer in advance.

about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

I have come up with one solution:

classKey={
 'classA': 'C',
 'classB': 'B',
 'classC': 'B+',
 'classD': 'D+',
 'classE': 'C+',
 'classF': 'A+',
};
scoreKey={
"A+" : 4.5,
"A" : 4,
"B+" : 3.5,
"B" : 3,
"C+" : 2.5,
"C" : 2,
"D+" : 1.5,
"D" : 1,
"F" : 0
};
function classScore(classKey,scoreKey){
  classKeyDup=classKey; //duplicate of class key, doesn't change original
  for (x in classKeyDup){ //Iterate through classes marks
    for(y in scoreKey){ //Iterate through scoreKey
      if (classKeyDup[x]==y){ //Check the mark of the class
        classKeyDup[x]=scoreKey[y]; //Apply Fixed score
      }
    }
  }
  return classKeyDup;
}
console.log(classScore(classKey,scoreKey))

about 4 years ago · Juan Pablo Isaza Relatório

0

Assuming that I understood your post correctly, this is my solution.

const testResults = {
  classA: "C",
  classB: "B",
  classC: "B+",
  classD: "D+",
  classE: "C+",
  classF: "A+",
};

const testedClasses = ["classE", "classD", "classO", "classP"];

function getScoreFromGrade(grade) {
  // convert a letter grade to a score
  // a grade with a + in it adds 0.5 to the score
  // any invalid grade or an f is a zero.

  const scores = { a: 4, b: 3, c: 2, d: 1 };

  const gradeParts = grade.toLowerCase().split("");
  const currentGrade = gradeParts[0];
  return !currentGrade in scores
    ? 0
    : scores[currentGrade] + (gradeParts[1] === "+" ? 0.5 : 0);
}

function getExamScores(testResults, testedClasses) {
  return testedClasses.reduce((retval, currentClass) => {
    let score =
      currentClass in testResults
        ? getScoreFromGrade(testResults[currentClass])
        : 0;
    retval[currentClass] = score;
    return retval;
  }, {});
}

console.log(getExamScores(testResults, testedClasses)) // { classE: 2.5, classD: 1.5, classO: 0, classP: 0 }
about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda