stuart-d2
11/10/2014 - 7:26 PM

indexOf function IV : If string A contains a instance of String B, it will only return String A. Else, If string A does not contain an ins

indexOf function IV : If string A contains a instance of String B, it will only return String A. Else, If string A does not contain an instance of String B, it will return both String A and String B. • Use Case : This is a solution to the problem where for UI display purposes, I want to reduce duplicates. For example, we have UI cosmetic issues where seriesName is 'Scion-tC' and grade level is 'tC'. Thus when they were concatenated in the UI, would display 'Scion-tC tC'. Now, the secondary instance of 'tC' will be suppressed for a clean look. If grade level is something else, like 'Premium', the entire string will display 'Scion-tC Premium'.

function seriesFormatter (series, grade) {
    var result;  
    if (series.indexOf(grade.toLowerCase()) >= 0) {
        var result = series;
    } else {
        var result = series + " " + grade;
    }
    return result;  
}

var seriesGradeCheck = seriesFormatter("scion-tc", "TC");
document.write(seriesGradeCheck);