javascriptjavascriptcore

Look for characters and if exists increment 1


I am trying to create a folder using javascript but before I do that I need to check if the folder exists and if it does increase the number by 1.

Here's my input name: The ABC Group

I want javascript to remove ‘The’ and move it to the end and then finally add a code based on the first 4 characters followed by a number. If the first 4 characters don’t exist in the directory then it will start 01 and increment from there on.

Here's what I want to output:

I.e. ABC Group, The (ABCG01)

I am new to javascript but I have so far worked out how to do everything apart from the number part which I am stuck on.

Here's my code:

var clientName = "the ABC company"; 

// Change Case - START
const toCapitaliseCase = (phrase) => {
return phrase
.split(' ')
.map(word => word.charAt(0).toUpperCase() + word.slice(1))
.join(' ');
};

let capitalise = toCapitaliseCase(clientName);
// Change Case - END
// Format Client Name if starts with 'The' - START)

if (capitalise.startsWith('The ')) {  
let words = capitalise.split(' ');
let the = words[0];
let theSlice = capitalise.slice(4);
let comma = ', ';
let name = theSlice.concat('', comma, the);
let name2 = name.replace(/[^0-9a-zA-Z]/g, "");
let theSlice2 = name2.slice(0,4);
var upper = theSlice2.toUpperCase(); // output - "i am a crazy string, make me normal!"
let numbecccr = '101';
let theSlice3 = numbecccr.slice(1);



let FullCompiledName = theSlice.concat('', comma, the, ' (',upper, theSlice3, ')');

console.log(FullCompiledName);
// Format Client Name - START

}

Solution

  • I put your code into a function, in which I counted the number up each time the function gets called.

    var clientName = "the ABC company"; 
    
    function createName(clientName) {
        this.number = this.number || 0;
        // Change Case - START
        const toCapitaliseCase = (phrase) => {
            return phrase
            .split(' ')
            .map(word => word.charAt(0).toUpperCase() + word.slice(1))
            .join(' ');
        };
    
        let capitalise = toCapitaliseCase(clientName);
        // Change Case - END
        // Format Client Name if starts with 'The' - START)
    
        if (capitalise.startsWith('The ')) {  
            let words = capitalise.split(' ');
            let the = words[0];
            let theSlice = capitalise.slice(4);
            let comma = ', ';
            let name = theSlice.concat('', comma, the);
            let name2 = name.replace(/[^0-9a-zA-Z]/g, "");
            let theSlice2 = name2.slice(0,4);
            var upper = theSlice2.toUpperCase(); // output - "i am a crazy string, make me normal!"
            this.number++;
            let num = this.number;
            if(('' + num).length == 1) {
                num = '0' + num;
            }
    
    
    
            let FullCompiledName = theSlice.concat('', comma, the, ' (',upper, num, ')');
    
            return FullCompiledName;
        }
    }
    
    console.log(createName(clientName));
    console.log(createName(clientName));