Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function solve(string) {
- let pascalArray = [];
- let regex = /[A-Z]/g;
- let found = string.match(regex);
- let currentWord = '';
- //found = [ 'S', 'M', 'I', 'Y', 'C', 'H', 'H', 'Y', 'C', 'O', 'Y', 'C' ]
- for (let i = 0; i < found.length; i++) {
- let firstIndex = string.indexOf(found[i]);
- let lastIndex = string.indexOf(found[i + 1], firstIndex + 1);
- if (lastIndex !== -1) {
- currentWord = string.substring(firstIndex, lastIndex);
- } else {
- currentWord = string.substring(firstIndex);
- }
- pascalArray.push(currentWord);
- string = string.substring(lastIndex);
- }
- pascalArray = pascalArray.filter(e => e);
- console.log(pascalArray.join(', '));
- }
- /*function solve(string) {
- const isUpperCase = (symbol) => {
- return symbol === symbol.toUpperCase();
- };
- let pascalArray = [];
- let currentWord = string[0];
- for (let i = 1; i < string.length; i++) {
- if (isUpperCase(string[i])) {
- pascalArray.push(currentWord);
- currentWord = string[i];
- } else {
- currentWord += string[i];
- }
- }
- pascalArray.push(currentWord);
- console.log(pascalArray.join(', '));
- }*/
Advertisement
Add Comment
Please, Sign In to add comment