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.
CodePudding user response:
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 }
CodePudding user response:
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))