Would you like to clone this notebook?

When you clone a notebook you are able to make changes without affecting the original notebook.

Cancel

checkLength

node v10.24.1
version: 3.0.0
endpointsharetweet
// Check if the number at the end of the string is equal to the length of the string without the number function checkLength(str) { } console.log(checkLength('october7')) // returns true console.log(checkLength('october11')) // returns false console.log(checkLength('0ctober7')) // returns true
Loading…

1 comment

  • posted 3 years ago by xrhoys
    // Check if the number at the end of the string is equal to the length of the string without the number var numberPattern = /\d+/g; function checkLength(str) { const numbers = str.match(numberPattern); if(numbers.length === 0) return false; console.log(numbers) let lastNumber; try { lastNumber = parseInt(numbers[numbers.length - 1]); } catch (e) { console.error(e); } if(lastNumber === str.length - lastNumber.toString().length) { return true; } else { return false; } } console.log(checkLength('october7')) // returns true console.log(checkLength('october11')) // returns false console.log(checkLength('0ctober7')) // returns true

sign in to comment