0

Hello I am trying to implement automated test scripts using NewRelic. I am engineering javascript such that I print the last character of a field on the page that is always 8 characters. I have this:

var last_character=page_value.charAt(8);
console.log('test last_character='+last_character)

but it only prints 'test last_character='! Is this a javascript variable scoping issue? I have never understood that!

asked Apr 25, 2017 at 17:43
2
  • 1
    There is a discrepancy between your nick (user name) and your skill level :-) Commented Apr 25, 2017 at 19:51
  • This is a really basic question: javascript starts counting the index at 0 not 1. So you need charAt(7) Commented Apr 26, 2017 at 11:24

1 Answer 1

0

I think you want to use .charAt(7) since js starts indexing at position 0.

answered Apr 25, 2017 at 18:15
3
  • Thank you! This was the problem. I wish javascript gave an error rather than an un-usable value! Commented Apr 25, 2017 at 19:54
  • @SrGuy Since you know it's an 8 character string, you can do this. You may want to do str.charAt(str.length - 1) in the future in case the string changes on you. Commented Apr 25, 2017 at 21:40
  • string.slice(-1) is a better approach, since it is cross-browser. Commented Apr 26, 2017 at 12:39

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.