2

This is the code I currently have. I am having issues displaying the ordinals as I am not sure what for loop to put then in. Can someone please assist?

var bandsString = "";
var newBandString = "";
var bandsListArray = ["Linkin Park", "Atmosphere", "Rage Against The Machine", "Immortal  Technique", "System of a Down"];

for (var i = 0; i < bandsListArray.length; i++) {
    var currentBand = bandsListArray[i];
    bandsString += "<li> My #" + [i + 1] + " band is " + currentBand + "</li>";
}

var bands = i;
if (bands == 0) {
    bands = i + "st";;
} else if (bands == 1) {
    bands = i + "nd";
} else if (bands == 2) {
    bands = i + "rd";
} else if (bands == 3) {
    bands = i + "th";
} else if (bands == 4) {
    bands = i + "th";
} else {

}

for (var i = 0; i < bandsListArray.length; i++) {
    var newBandList = bandsListArray[i];

    newBandString += "<li>My # " + [i + 1] + " band is " + currentBand + "</li>";
}


document.write(bandsString);
document.write(newBandString);
Eric Semwenda
  • 436
  • 6
  • 16
  • What do you want the display to look like? – GeekSilva Jun 22 '17 at 02:51
  • I just want it to display the arrays in the html when I open the browser, I'm trying to display two different lists, which will somewhat be the same except one will say "My #1 band is..." and the other will say "My 1st band is..." and so on with the rest of the arrays. – Henry Merida Jun 22 '17 at 02:54
  • You're close... you need to move your ordinals into the second for loop, so that i t gets detected every time. Also you should use normal brackets (ie`()`) instead of square brackets `[]` to enclose your arithmetic... – Shadow Jun 22 '17 at 03:00
  • So would I place my ordinals after I declare my newBandString? and also, when running this code it only displays System of a Down below the first list
    – Henry Merida Jun 22 '17 at 03:02
  • Looks like your if/else if conditions are mismatched with their bodies, such that you'll get `"0st", "1nd", "2rd", "3th"`. – nnnnnn Jun 22 '17 at 03:03
  • Oh okay I forgot to put the +1, but that still doesn't display the bottom list the way I'm trying to – Henry Merida Jun 22 '17 at 03:08
  • What do you actually want the output to look like? – Clonkex Jun 22 '17 at 03:13
  • Okay so now it's fixed seems like I put newBandList instead of what I meant to put which was, newBandString, so now the bottom list does display but i need the numbers on the bottom list to be in ordinal rather than how it is displayed on the first list.For example, I need the numbers to be listed as (1st, 2nd, 3rd, 4th, 5th) – Henry Merida Jun 22 '17 at 03:18
  • Yeah I thought that was a mistake. I've posted an answer, see if that helps you. – Clonkex Jun 22 '17 at 03:19
  • Okay thats exactly what I needed! Thanks alot! Can you explain to me how the function works. What does the %10 / %100 do? And where did the "j" & "k" come from. I see that it works perfectly I just want to understand why it works. – Henry Merida Jun 22 '17 at 03:23
  • Yep, I'll edit my answer with some explanations. – Clonkex Jun 22 '17 at 03:23
  • Okay thank you very much – Henry Merida Jun 22 '17 at 03:26
  • @HenryMerida First, I don't get notified about comments on your question unless you tag me (like I did to you in this comment). If you commented on my answer I'd be notified even if you didn't tag me, but I'd have to tag you to make you get notified. Just a tip on how to use stack overflow :) Second, I've edited my question to expand on how the function works. Hopefully it's clear enough; if you need a better explanation just let me know and I'll see what I can do :) – Clonkex Jun 22 '17 at 03:35

3 Answers3

1

I'm not exactly sure what you're trying to do, but does this help? Instead of manually working out how to put the correct ordinal suffix ('st', 'nd', 'rd', etc.) on the numbers, I googled a function.

The ordinal_suffix_of function works like this:

This first part is the start of the function. Hopefully this is self-explanatory. We're creating a function that accepts one parameter, that will be named i inside the function.

function ordinal_suffix_of(i) {

This next part defines j and k. You can declare multiple variables while only typing var once by using commas, as this code does:

var j = i % 10,
    k = i % 100;

This would do exactly the same thing:

var j = i % 10;
var k = i % 100;

The percent sign (%) is called "modulus" and is a mathematical operator. What it does is divide the left operand by the right operand and returns the remainder. So in the case of j, it's being assigned remainder of dividing i by 10. If i was 14, for example, j would be 4. If i was 179, j would be 9. The same thing happens for k except that it's divided by 100 instead of 10.

What it's effectively doing is extracting the last 1 digit (for j) and the last 2 digits (for k) and then just checking (in the if statements) what ordinal suffixes they should have.

The reason for k is that you always want numbers ending in 1, 2, or 3 to have the suffixes st, nd , and rd except when the number ends in 11, 12, or 13, in which case it should get the suffix th. So for example, 112 should be 112th, and 313513 should be 313513th. k handles this by making it so that if the number ends in 11, 12, or 13 we only add th. Note that 11 % 100 will result in 11, so this works for 11, 12, and 13 as well.

var bandsString = "";
var newBandString = "";
var bandsListArray = ["Linkin Park", "Atmosphere", "Rage Against The Machine", "Immortal  Technique", "System of a Down"];

for (var i = 0; i < bandsListArray.length; i++) {
 var currentBand = bandsListArray[i];
 bandsString += "<li> My #" + (i + 1) + " band is " + currentBand + "</li>";
}

for (var i = 0; i < bandsListArray.length; i++) {
 var newBandList = bandsListArray[i];

 newBandString += "<li>My " + ordinal_suffix_of(i + 1) + " band is " + newBandList + "</li>";
}

function ordinal_suffix_of(i) {
    var j = i % 10,
        k = i % 100;
    if (j == 1 && k != 11) {
        return i + "st";
    }
    if (j == 2 && k != 12) {
        return i + "nd";
    }
    if (j == 3 && k != 13) {
        return i + "rd";
    }
    return i + "th";
}


document.write(bandsString);
document.write(newBandString);
Clonkex
  • 3,373
  • 7
  • 38
  • 55
  • okay I understand it now, but for the K why is it necessary if all of the if statements show that it's not equal to K? Sorry by the way I'm new to coding so its a bit hard to understand it right away – Henry Merida Jun 22 '17 at 03:40
  • In other words, couldn't this function work without involving the variable K? – Henry Merida Jun 22 '17 at 03:41
  • @HenryMerida Edited my answer. Let me know if that doesn't explain it well enough for you :) – Clonkex Jun 22 '17 at 03:48
  • Oh Okay that helped understand it much better thank you for your time I really appreciate it. You, and everyone else where a great help! – Henry Merida Jun 22 '17 at 03:50
  • @HenryMerida No worries man! I enjoy helping people understand programming, and all the cool and interesting things you can do with it :) Plus I'm at work and this is a lot more interesting than setting up products on a website haha! – Clonkex Jun 22 '17 at 03:52
  • 1
    Haha! Yeah that's one of the things I've come to love about coding, the problem solving. It can be a bit of a headache at times but once it clicks its a great feeling. So you'll probably see more questions coming from me soon. – Henry Merida Jun 22 '17 at 03:57
0

Try this, just make the code more simple

var bandsString = "";
var newBandString = "";
var bandsListArray = ["Linkin Park", "Atmosphere", "Rage Against The Machine", "Immortal  Technique", "System of a Down"];

for (var i = 0; i < bandsListArray.length; i++) {
   var currentBand = bandsListArray[i];
   bandsString += "<li> My #" + [i + 1] + " band is " + currentBand + "</li>";        
   newBandString += "<li> My #" + numeric(i+1) + " band is " + currentBand + "</li>";
}

function numeric(i){
   var bands = i+"th";
  if (i == 1) {
    bands = i + "st";;
  } else if (i == 2) {
    bands = i + "nd";
  } else if (i == 3) {
    bands = i + "rd";
  } 
  return bands;
}
document.write(bandsString);
document.write(newBandString);
Timothy Lee
  • 768
  • 4
  • 15
  • 1
    Be aware that that `numeric` function only works for numbers as high as 20. It will output `21th`, `22th`, `23th`, etc. Not knocking your arrangement of the OP's code, @TimothyLee, just making sure people seeing this realise :) – Clonkex Jun 22 '17 at 03:39
  • Oh thanks for reminding! It's 21st,22nd,23rd,24th...etc.So change the numeric() to your ordinal_suffix_of() will be correct. – Timothy Lee Jun 22 '17 at 03:45
  • Sure will! But it's not my `ordinal_suffix_of`, I got it from [this answer](https://stackoverflow.com/a/13627586/2288578). – Clonkex Jun 22 '17 at 03:49
0

If this is what you are trying to achieve is:

My 2nd band is Atmosphere

Then this how you might achieve it

var bandsString = "";
var newBandString = "";
var bandsListArray = ["Linkin Park", "Atmosphere", "Rage Against The Machine", "Immortal  Technique", "System of a Down"];

for (var i = 0; i < bandsListArray.length; i++) {
    var currentBand = bandsListArray[i];
    bandsString += "<li> My #" + (i + 1) + " band is " + currentBand + "</li>";
    newBandString += "<li>My " + numberSuffix(i + 1) + " band is " + currentBand + "</li>";
}

function numberSuffix(number){
        if(number % 10==1) return number+'st';
        if(number % 10==2) return number+'nd';
        if(number % 10==3) return number+'rd';
        return number+'th';
}

document.write(bandsString);
document.write(newBandString);
Sholeman
  • 1
  • 2