I would like to dynamically generate a string of text based on a current day. So, for example, if it is day 1 then I would like my code to generate = “Its the <dynamic>1*<dynamic string>st</dynamic string>*</dynamic>”.
There are 12 days in total so I have done the following:
I’ve set up a for loop which loops through the 12 days.
In my html I have given my element a unique id with which to target it, see below:
JavaScriptx21<h1 id="dynamicTitle" class="CustomFont leftHeading shadow">On The <span></span> <em>of rest of generic text</em></h1>
2
Then, inside my for loop I have the following code:
JavaScript110101$("#dynamicTitle span").html(i);
2var day = i;
3if (day == 1) {
4day = i + "st";
5} else if (day == 2) {
6day = i + "nd"
7} else if (day == 3) {
8day = i + "rd"
9}
10
UPDATE
This is the entire for loop as requested:
JavaScript
1
39
39
1
$(document).ready(function () {
2
for (i = 1; i <= 12; i++) {
3
var classy = "";
4
if (daysTilDate(i + 19) > 0) {
5
classy = "future";
6
$("#Day" + i).addClass(classy);
7
$("#mainHeading").html("");
8
$("#title").html("");
9
$("#description").html("");
10
} else if (daysTilDate(i + 19) < 0) {
11
classy = "past";
12
$("#Day" + i).addClass(classy);
13
$("#title").html("");
14
$("#description").html("");
15
$("#mainHeading").html("");
16
$(".cta").css('display', 'none');
17
$("#Day" + i + " .prizeLink").attr("href", "" + i + ".html");
18
} else {
19
classy = "current";
20
$("#Day" + i).addClass(classy);
21
$("#title").html(headings[i - 1]);
22
$("#description").html(descriptions[i - 1]);
23
$(".cta").css('display', 'block');
24
$("#dynamicImage").attr("src", ".." + i + ".jpg");
25
$("#mainHeading").html("");
26
$(".claimPrize").attr("href", "" + i + ".html");
27
$("#dynamicTitle span").html(i);
28
var day = i;
29
if (day == 1) {
30
day = i + "st";
31
} else if (day == 2) {
32
day = i + "nd"
33
} else if (day == 3) {
34
day = i + "rd"
35
} else if (day) {
36
}
37
}
38
}
39
Advertisement
Answer
The rules are as follows:
- st is used with numbers ending in 1 (e.g. 1st, pronounced first)
- nd is used with numbers ending in 2 (e.g. 92nd, pronounced ninety-second)
- rd is used with numbers ending in 3 (e.g. 33rd, pronounced thirty-third)
- As an exception to the above rules, all the “teen” numbers ending with 11, 12 or 13 use -th (e.g. 11th, pronounced eleventh, 112th, pronounced one hundred [and] twelfth)
- th is used for all other numbers (e.g. 9th, pronounced ninth).
The following JavaScript code (rewritten in Jun ’14) accomplishes this:
JavaScript
1
15
15
1
function ordinal_suffix_of(i) {
2
var j = i % 10,
3
k = i % 100;
4
if (j == 1 && k != 11) {
5
return i + "st";
6
}
7
if (j == 2 && k != 12) {
8
return i + "nd";
9
}
10
if (j == 3 && k != 13) {
11
return i + "rd";
12
}
13
return i + "th";
14
}
15
Sample output for numbers between 0-115:
JavaScript
1
117
117
1
0 0th
2
1 1st
3
2 2nd
4
3 3rd
5
4 4th
6
5 5th
7
6 6th
8
7 7th
9
8 8th
10
9 9th
11
10 10th
12
11 11th
13
12 12th
14
13 13th
15
14 14th
16
15 15th
17
16 16th
18
17 17th
19
18 18th
20
19 19th
21
20 20th
22
21 21st
23
22 22nd
24
23 23rd
25
24 24th
26
25 25th
27
26 26th
28
27 27th
29
28 28th
30
29 29th
31
30 30th
32
31 31st
33
32 32nd
34
33 33rd
35
34 34th
36
35 35th
37
36 36th
38
37 37th
39
38 38th
40
39 39th
41
40 40th
42
41 41st
43
42 42nd
44
43 43rd
45
44 44th
46
45 45th
47
46 46th
48
47 47th
49
48 48th
50
49 49th
51
50 50th
52
51 51st
53
52 52nd
54
53 53rd
55
54 54th
56
55 55th
57
56 56th
58
57 57th
59
58 58th
60
59 59th
61
60 60th
62
61 61st
63
62 62nd
64
63 63rd
65
64 64th
66
65 65th
67
66 66th
68
67 67th
69
68 68th
70
69 69th
71
70 70th
72
71 71st
73
72 72nd
74
73 73rd
75
74 74th
76
75 75th
77
76 76th
78
77 77th
79
78 78th
80
79 79th
81
80 80th
82
81 81st
83
82 82nd
84
83 83rd
85
84 84th
86
85 85th
87
86 86th
88
87 87th
89
88 88th
90
89 89th
91
90 90th
92
91 91st
93
92 92nd
94
93 93rd
95
94 94th
96
95 95th
97
96 96th
98
97 97th
99
98 98th
100
99 99th
101
100 100th
102
101 101st
103
102 102nd
104
103 103rd
105
104 104th
106
105 105th
107
106 106th
108
107 107th
109
108 108th
110
109 109th
111
110 110th
112
111 111th
113
112 112th
114
113 113th
115
114 114th
116
115 115th
117