0

I have an array that contains three different strings for example:

[ 'obtain wealth1', 'obtain dreams3', 'obtain discretion3' ]

The array is created from a function I've written.

What I want to do next is compare the last characters of the strings to one another (the numbers) to see if I produce a match.

How would I go about doing this?

Thanks for the help.

2
  • What did you try? What are you having trouble with? Commented Jan 14, 2013 at 19:42
  • do u just wish to see if there is a repetition in the numbers?? Commented Jan 14, 2013 at 19:44

2 Answers 2

3

First loop through the array and determine (with an if statement) if the elements held at the positions in the array have, as its last character, a value equal to the number you wish. You can add else if statements to check for multiple conditions. We use the slice method with an argument of -1 to check for the rightmost character in the string.

for (var i = list.length; i--;) {
    if (list[i].slice(-1) == the_number) {

    } else {}
}
Sign up to request clarification or add additional context in comments.

3 Comments

could you explain what you're suggesting here?
@michelFeldheim We loop through the array and see if the last character in the element at index i is equal to the_number (which is a variable equal to a number). Is this what you're wanting.
Yip, perfect :) add this to the answer
0

With string functions for example: http://www.w3schools.com/jsref/jsref_obj_string.asp

var a = [ 'obtain wealth1', 'obtain dreams3', 'obtain discretion3' ];
var length = a.length;
var lastchar = a.charAt(length - 1);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.