Home > Mobile >  How to draw dynamically draw units out of an array and pair them with a predefined number value?
How to draw dynamically draw units out of an array and pair them with a predefined number value?

Time:02-10

So I'm working on a homework assignment where I'm supposed track the "how many points a sports team has earned in a season" by writing a function called getTotalPoints that accepts a string of letters and returns the total points earned. These letters would be any series of "w" (wins) "d" (draws) and/or "l"(losses).

The assignment includes a pre-defined number value to each letter (w = 3, d = 1, and l =0), a helper function to draw out these values getPointsFromResult, and the function with a string param passed, getTotalPoints('wwdl') to test the getTotalPoints function I wrote works properly.

I got as a far as turning the string into an array and then tried to iterate on that array. The correct answer should be 7, but I am getting a result of 16, because it seems that forEach loop is iterating everything x's 4 (4 being the length of the array) As a result of 4 sets of 3 1 0 (essentially 4 x 4).

I have no idea how to move forward from here and am completely stuck.

const RESULT_VALUES = {
  w: 3,
  d: 1,
  l: 0
}

/**
 * Takes a single result string and (one of 'w', 'l', or 'd') 
 * and returns the point value
 * 
 * @param {string} result 
 * @returns {number} point value
 */

function getPointsFromResult(result) {
  return RESULT_VALUES[result];
}

// Create getTotalPoints function which accepts a string of results
// including wins, draws, and losses i.e. 'wwdlw'
// Returns total number of points won


let getTotalPoints = function(pointsString) {
  let pointsArray = Array.from(pointsString);
  console.log(pointsArray);

  let sum = 0

  pointsArray.forEach(function(result) {
    console.log(result)
    let wins = getPointsFromResult('w');
    let draws = getPointsFromResult('d');
    let losses = getPointsFromResult('l');


    console.log(wins);
    console.log(draws);
    console.log(losses);

    sum  = wins;
    sum  = draws;
    sum  = losses;
  })

  return sum;

};

// Check getTotalPoints
console.log(getTotalPoints('wwdl')); // should equal 7

CodePudding user response:

How about something like this:

const RESULT_VALUES = {
  w: 3,
  d: 1,
  l: 0
}

function getPointsFromResult(result) {
  return RESULT_VALUES[result];
}

let getTotalPoints = function(pointsString) {
  let pointsArray = Array.from(pointsString);
  // Put value of each letter into a results array:
  let results = pointsArray.map(result => getPointsFromResult(result));
  // Sum all values in above results array to get total.
  let sum = results.reduce((a, b) => a   b, 0);

  return sum;
};

console.log(getTotalPoints('wwdl')); // should equal 7

CodePudding user response:

split() "wwdl" string into an array:

'wwdl'.split('') //['w', 'w', 'd', 'l']

Then get each key of the object ('w', 'd', 'l') and get the value (3, 1, 0).

 object['w'] // first iteration 3
 object['w'] // second iteration 3
 object['d'] // third iterayion 1
 object['l'] // fourth iteration 0

.reduce() can add the values and return a sum:

  sum   current value

In the example below log(data) is just a helper function to log to console easier.

const log = data => console.log(JSON.stringify(data));
const record = `wwdl`;

const totalScore = record => {
  const scoring = {
    w: 3,
    d: 1,
    l: 0
  };

  let recArr = record.split('');
  log('String as an Array:');
  log(recArr);

  return recArr.reduce((sum, cur) => {
    log('Current Key: '   cur   ': return: '   scoring[cur]);
    return sum   scoring[cur];
  }, 0);
};

log('Total Score: '   totalScore(record));
.as-console-row::after {
  width: 0;
  font-size: 0;
}

.as-console-row-code {
  width: 100%;
  word-break: break-word;
}

  • Related