Welcome, Guest: Register On Nairaland / LOGIN! / Trending / Recent / New
Stats: 3,148,569 members, 7,801,620 topics. Date: Thursday, 18 April 2024 at 06:34 PM

You Can Do This Too - Programming - Nairaland

Nairaland Forum / Science/Technology / Programming / You Can Do This Too (602 Views)

(2) (3) (4)

(1) (Reply) (Go Down)

You Can Do This Too by Maskyy(m): 5:32am On Jun 23, 2020
Let's play a bit with algorithm and problem-solving. Your comment is a contribution even if you can't attempt it.

const people = [
    {firstName: 'Sam', lastName: 'Hughes', DOB: '07/07/1978'},
    {firstName: 'Terri', lastName: 'Bishop', DOB: '07/04/1989'},
    {firstName: 'Jar', lastName: 'Burke', DOB: '11/01/1985'},
    {firstName: 'Julio', lastName: 'Miller', DOB: '12/02/1975'},
    {firstName: 'Chester', lastName: 'Flores', DOB: '15/03/1988'},
    {firstName: 'Madison', lastName: 'Marshall', DOB: '22/09/1980'},
    {firstName: 'Gabriella', lastName: 'Steward', DOB: '26/08/1990'},
    {firstName: 'Ava', lastName: 'Pena', DOB: '02/11/1986'},
]

Using an array method solve the following.

1. What's the age of each person in the array.
2. Get the list of the people in the array ordered from youngest to oldest.

Happy coding and learning.

1 Like

Re: You Can Do This Too by Maskyy(m): 5:40am On Jun 23, 2020
The above helps to understand JSON and how to work with one when using API.
Re: You Can Do This Too by Taofeekdboy(m): 11:46am On Jun 23, 2020
OP, this is the only method i can come up with as i do not want to use third party library for the date time.

function parseDate(input) {
var parts = input.match(/(\d+)/g);
return new Date(parts[2], parts[1]-1, parts[0]);
}

const people = [
{firstName: 'Sam', lastName: 'Hughes', DOB: '07/07/1978'},
{firstName: 'Terri', lastName: 'Bishop', DOB: '07/04/1989'},
{firstName: 'Jar', lastName: 'Burke', DOB: '11/01/1985'},
{firstName: 'Julio', lastName: 'Miller', DOB: '12/02/1975'},
{firstName: 'Chester', lastName: 'Flores', DOB: '15/03/1988'},
{firstName: 'Madison', lastName: 'Marshall', DOB: '22/09/1980'},
{firstName: 'Gabriella', lastName: 'Steward', DOB: '26/08/1990'},
{firstName: 'Ava', lastName: 'Pena', DOB: '02/11/1986'},
]
people.map(user => {
const minutes = 1000 * 60;
const hours = minutes * 60;
const days = hours * 24;
const years = days * 365;
const tody = Date.parse(new Date)
const today = Date.parse(parseDate(user.DOB))
const current = tody - today
console.log(Math.round(current/years))
})

const sortedList = people.sort((a, b) => {
return parseDate(a.DOB).getFullYear() < parseDate(b.DOB).getFullYear() ? -1 : 1
})
console.log(sortedList)

1 Like

Re: You Can Do This Too by Coder2Client(m): 1:23pm On Jun 23, 2020
JSON Array

1 Like

Re: You Can Do This Too by Maskyy(m): 3:51pm On Jun 23, 2020
Taofeekdboy:
OP, this is the only method i can come up with as i do not want to use third party library for the date time.

function parseDate(input) {
var parts = input.match(/(\d+)/g);
return new Date(parts[2], parts[1]-1, parts[0]);
}

const people = [
{firstName: 'Sam', lastName: 'Hughes', DOB: '07/07/1978'},
{firstName: 'Terri', lastName: 'Bishop', DOB: '07/04/1989'},
{firstName: 'Jar', lastName: 'Burke', DOB: '11/01/1985'},
{firstName: 'Julio', lastName: 'Miller', DOB: '12/02/1975'},
{firstName: 'Chester', lastName: 'Flores', DOB: '15/03/1988'},
{firstName: 'Madison', lastName: 'Marshall', DOB: '22/09/1980'},
{firstName: 'Gabriella', lastName: 'Steward', DOB: '26/08/1990'},
{firstName: 'Ava', lastName: 'Pena', DOB: '02/11/1986'},
]
people.map(user => {
const minutes = 1000 * 60;
const hours = minutes * 60;
const days = hours * 24;
const years = days * 365;
const tody = Date.parse(new Date)
const today = Date.parse(parseDate(user.DOB))
const current = tody - today
console.log(Math.round(current/years))
})

const sortedList = people.sort((a, b) => {
return parseDate(a.DOB).getFullYear() < parseDate(b.DOB).getFullYear() ? -1 : 1
})
console.log(sortedList)

Wow . pls can I see your result screenshot.
Re: You Can Do This Too by Maskyy(m): 3:51pm On Jun 23, 2020
Coder2Client:
JSON Array

Yea
Re: You Can Do This Too by Taofeekdboy(m): 4:04pm On Jun 23, 2020
That's the screenshot, this can also be done with forEach but I so much like map grin

1 Like

Re: You Can Do This Too by etoluw: 5:55pm On Jun 23, 2020
people.sort( function(a,b) {
var aDate = a.DOB.split('/'); var bDate = b.DOB.split('/');
if ( aDate[2] < bDate[2] ) return true; if ( aDate[2] > bDate[2] ) return false; if ( aDate[1] < bDate[1] ) return true; if ( aDate[1] > bDate[1] ) return false; if ( aDate[0] > bDate[0] ) return true; return false;
} );

1 Like

Re: You Can Do This Too by Maskyy(m): 6:17pm On Jun 23, 2020
This is my solution too

// for the current year 2020
let y = new Date()
let cY = y.getFullYear()


1.
people.map(data =>{
return (`${cY -
parseInt(data.DOB.substr(-4))`);
})

2.
people.sort((a,b)=>{
let r = cY - parseint(a.DOB.substr(-4)),
s = cY - parseInt(b.DOB.substr(-4));
return r-s;
}).map(data=>{
console.log(`${data.firstName}
${data.lastName},
${cY - parseInt(data.DOB.substr(-4))}`)
})
Re: You Can Do This Too by Maskyy(m): 6:24pm On Jun 23, 2020
Thanks for the comments and feedbacks. More are coming. Happy coding.
Re: You Can Do This Too by Maskyy(m): 6:44pm On Jun 23, 2020
.
Re: You Can Do This Too by Maskyy(m): 1:30pm On Jun 24, 2020
Let's play around with this too. Googling is allowed but copy paste of solution is not welcomed. Good luck.


Given n, take the sum of the digits of n. If that value has more than one digit, continue reducing in this way until a single-digit number is produced. This is only applicable to the natural numbers.
Example below.

16 --> 1 + 6 = 7

942 --> 9 + 4 + 2 = 15 --> 1 + 5 = 6

132189 --> 1 + 3 + 2 + 1 + 8 + 9 = 24 --> 2 + 4 = 6

493193 --> 4 + 9 + 3 + 1 + 9 + 3 = 29 --> 2 + 9 = 11 --> 1 + 1 = 2

1 Like 1 Share

Re: You Can Do This Too by Maskyy(m): 3:28pm On Jun 25, 2020
What's your solution so far?
Coder2Client
Taofeekdboy
Re: You Can Do This Too by Taofeekdboy(m): 1:11am On Jun 26, 2020
I have been busy, Just got the time. I will use recursive function , as this makes the code cleaner and I am using python this time. I am quite familiar with python because I use it to code my backend, javascript for frontend.

def get_single_digit(n):
y = str(n)
x = 0
for i in y:
x += int(i)
if x > 9:
return get_single_digit(x)
else:
print(x)
return x

I can break it down;
By calling get_single_digit() with a parameter, the function will convert the digit(s) into string because of limitation of number variable in python,
setting a variable x = 0; because it will be used for the looping condition.
then I loop through the string and adding the looping variable being converted to integer to x which produce a result be it 1 or more digits.
I check for a condition whereby if x > 9, it means if x is greater than 9 then it can be any two digits which can be re-added again, I used recursive function to return the condition which call itself until else condition is met.
I do hope I explain in a little way.

1 Like 1 Share

Re: You Can Do This Too by modash(m): 1:51am On Jun 26, 2020
Maskyy:
Let's play around with this too. Googling is allowed but copy paste of solution is not welcomed. Good luck.


Given n, take the sum of the digits of n. If that value has more than one digit, continue reducing in this way until a single-digit number is produced. This is only applicable to the natural numbers.
Example below.

16 --> 1 + 6 = 7

942 --> 9 + 4 + 2 = 15 --> 1 + 5 = 6

132189 --> 1 + 3 + 2 + 1 + 8 + 9 = 24 --> 2 + 4 = 6

493193 --> 4 + 9 + 3 + 1 + 9 + 3 = 29 --> 2 + 9 = 11 --> 1 + 1 = 2



function addDigits(n){
if(n < 10){
return n;
}
const sum = n.toString().split('').map(Number).reduce((acc,next) => acc + next);
return addDigits(sum);
}

1 Like 1 Share

Re: You Can Do This Too by modash(m): 1:52am On Jun 26, 2020
solution in javascript
Re: You Can Do This Too by Maskyy(m): 8:38am On Jun 26, 2020
modash:




function addDigits(n){
if(n < 10){
return n;
}
const sum = n.toString().split('').map(Number).reduce((acc,next) => acc + next);
return addDigits(sum);
}

What if the value is 11 and it output 1,1. How will your code make it 1+1 which should output 2?

And thanks for the contribution
Re: You Can Do This Too by modash(m): 11:17am On Jun 26, 2020
Maskyy:


What if the value is 11 and it output 1,1. How will your code make it 1+1 which should output 2?

And thanks for the contribution

yes 11 gives an output of two you can try it in your chrome console

1 Like

Re: You Can Do This Too by Maskyy(m): 1:22pm On Jun 26, 2020
modash:


yes 11 gives an output of two you can try it in your chrome console

Yea, part of code is not visible, maybe due to the use of Nl wrap[code].

Thanks for the contribution again
Re: You Can Do This Too by modash(m): 3:15pm On Jun 26, 2020
function addDigits(n){
if(n < 10){
return n;
}
const sum = n.toString().split('').map(Number).reduce((acc,next) => acc + next);
return addDigits(sum);
}

2 Likes 1 Share

Re: You Can Do This Too by Maskyy(m): 5:33pm On Jun 26, 2020
Thanks to contributors and ghost readers

(1) (Reply)

I Have Issues With Starting React / Body Fitness For Women / Java For Beginners Part 2

(Go Up)

Sections: politics (1) business autos (1) jobs (1) career education (1) romance computers phones travel sports fashion health
religion celebs tv-movies music-radio literature webmasters programming techmarket

Links: (1) (2) (3) (4) (5) (6) (7) (8) (9) (10)

Nairaland - Copyright © 2005 - 2024 Oluwaseun Osewa. All rights reserved. See How To Advertise. 31
Disclaimer: Every Nairaland member is solely responsible for anything that he/she posts or uploads on Nairaland.