1

I'm given an array of objects representing data about people. I need to filter names, which when add the ASCII representation of all characters in their first names, the result will be an odd number. e.g.: Sum of ASCII codes of letters in 'Aba' is: 65 + 98 + 97 = 260 which is an even number -- filter it out

My logic is as follows:

  1. get first names;
  2. convert each letter of the first name to ASCII code;
  3. add ASCII codes of each name;
  4. check, if the received number is odd;
  5. return names, which ASCII code number is odd.

I've completed 4 tasks of the above, but I do not know, how to connect items from different arrays. E.g.: I have an array of names: let names = ['Aba', 'Abb'], and array of odd/even numbers: let oddEven = [0, 1].

How can I make:

1) names[0] is oddEven[0]

2) if oddEven[0] > 0 { filter it out } ?

Thanks

my code:

function findOddNames(list) {

  let names = list.map((name) => name.firstName);
  let splittedNames = names.map((name) => name.split(''));
  let charCodes = splittedNames.map((arr) => arr.map((letter) => letter.charCodeAt(0)));
  let charCodesTotal = charCodes.map((arr) => arr.reduce((a, b) => a + b, 0));
  let oddEven = charCodesTotal.map((item) => item % 2);    
};

findOddNames([
  { firstName: 'Aba', lastName: 'N.', country: 'Ghana', continent: 'Africa', age: 21, language: 'Python' },
  { firstName: 'Abb', lastName: 'O.', country: 'Israel', continent: 'Asia', age: 39, language: 'Java' }
])

3 Answers 3

1

You can use filter like this. oddEven has the modulo values. Just filter the indexes from names array which have non-zero oddEven values. To get even values, use oddEven[i] === 0

function findOddNames(list) {
  let names = list.map((name) => name.firstName);
  let splittedNames = names.map((name) => name.split(''));
  let charCodes = splittedNames.map((arr) => arr.map((letter) => letter.charCodeAt(0)));
  let charCodesTotal = charCodes.map((arr) => arr.reduce((a, b) => a + b, 0));
  let oddEven = charCodesTotal.map((item) => item % 2);
  let oddNames = names.filter((n, i) => oddEven[i] !== 0)
  return oddNames;
};

const odd = findOddNames([
  { firstName: 'Aba', lastName: 'N.', country: 'Ghana', continent: 'Africa', age: 21, language: 'Python' },
  { firstName: 'Abb', lastName: 'O.', country: 'Israel', continent: 'Asia', age: 39, language: 'Java' }
])

console.log(odd)

Sign up to request clarification or add additional context in comments.

3 Comments

unbelievable!! Thank you very much!) By the way, could you please explain, what does _, mean?
@an_bozh sorry for adding that there. It's just a parameter name. You can use n or name
I'm accepting your answer, because it answers my question. Other solutions are better than mine, but they are not mine. I need to show my own solution. I'm not able to write code the way @NickParsons and Ele do. Great practice, a lot of useful information. Thank you all very much
0

This alternative uses the function filter to get only odd names and the function reduce to sum the ASCII code for each letter, and finally the operator % to check if the sum is even or odd.

let findOddNames = (arr) => {
  return arr.filter(({firstName}) => {
    return firstName.split('').reduce((a, letter) => letter.charCodeAt(0) + a, 0) % 2;
  });
};

let result = findOddNames([
  { firstName: 'Aba', lastName: 'N.', country: 'Ghana', continent: 'Africa', age: 21, language: 'Python' },
  { firstName: 'Abb', lastName: 'O.', country: 'Israel', continent: 'Asia', age: 39, language: 'Java' }
]);

console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }

Comments

0

Why not use .filter instead without creating separate arrays to hold each variation of your input array.

Below, I have used .filter to filter out your input array into your findOddNames array. If you want to keep a given element when using filter, you need to return true, if you don't want to keep it (ie sum of the firstName is even) then you can return false.

In the filter function, I have also made use of destructuring assignment, which allows me to easily pull out the firstName property from your input array. I have also used the spread syntax. This is an easy way to convert a string of characters, into an array of characters. Finally, I used .reduce on the array to get the sum of all the characters in the array.

See example below:

const findOddNames = arr => {
  return arr.filter(({firstName:n}) => {
    const sum = [...n].reduce((acc, letter) => acc + letter.charCodeAt(0), 0);
    return sum % 2 !== 0;  
  });
}

console.log(findOddNames([
  { firstName: 'Aba', lastName: 'N.', country: 'Ghana', continent: 'Africa', age: 21, language: 'Python' },
  { firstName: 'Abb', lastName: 'O.', country: 'Israel', continent: 'Asia', age: 39, language: 'Java' }
]));

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.