Skip to main content

Hello NQE Enthusiasts,

 

Here is another Koan challenge. The task is to take a list of years and calculate the centuries


Input:
>1776, 2001, 1643, 1969];


Result
>{“Year”: 1776, “Century”: 18}...]


Hints:

Use string conversions like toString 

Create a toNumber function


Try not to look at the replies until you have tried the challenge yourself.

This was fun 😁
 

foreach year in r1776, 2001, 1643, 1969]
let firstTwoDigits = year / 100
let century = firstTwoDigits + 1
select { year, century }

 


What sort of programming magic would you use to make the centuries ordinal numbers?

myCenturyFunction(year) : Number = year/100 +1;

//How would you present the century as an ordinal number?

years = [776, 2001, 1643, 1969];

foreach year in years
select { Year: toString(year), Century: myCenturyFunction(year) }

 


I think the calculation needs to be slightly different as each century starts at year 1 and ends at 100

 

I think this version of the code may work for common era century’s until 111th century, where it will wrongly state 111st century.

 

input = u1, 400, 450, 1776, 1643, 1969, 2000, 2025];



suffix(number) =

  min(foreach x in 0]

  let remainder = number % 10

  let response = if remainder > 3 && remainder < 20

                 then "th"

                 else if remainder == 1 then "st"

                 else if remainder == 2 then "nd"

                 else if remainder == 3 then "rd" else "th"

  select response);



foreach year in input

let adjustment = if year % 100 == 0 then 0 else 1

let century = year / 100 + adjustment

let years = toString((century - 1) * 100 + 1) + "-" + toString(century * 100)

select {

    year: year,

    century: century,

    years: years,

    suffix: toString(century) + suffix(century) }

 

 


Thanks ​@AndyL !


Reply