A Little Help With This JS Code. I Don't Know Why It Doesn't Work

A Little Help With This JS Code. I Don't Know Why It Doesn't Work
0

#1

I’m trying to capitalize the second word in a string. Example input: “one two three” should return “one TWO three”.
I keep getting an error on the “join” line that says “Not a Function.”

Any advice would be greatly appreciated.

function kungfoo(input) {
    
    var output = input.split(" ");
        input[1].toUpperCase;
        input.join(" ");
    
    return output;
}

#2

split returns an array, which you are assigning to the variable output. input is still a string, and output is what you want to be calling toUpperCase and join on.


#3

Thanks for the help. I get what you’re saying. So shouldn’t this work?
I changed it to this and I still get the error on the join line saying it’s not a function.

function kungfoo(input) {
    
var arr = input.split(" ");
    var output = arr[1].toUpperCase;
        output.join(" ");
    
    return output;
}

#4

No, you’re still not operating on the correct variable.

function kungfoo(input) {
    // This is valid and exactly what you want to do. The result of input.split() will be an array.
    var arr = input.split(" ");

    // This doesn't do anything because you're not actually calling the toUpperCase function.
    // You need to assign its result back to the array, not a new variable
    var output = arr[1].toUpperCase;
    console.log(output); // this will output "TWO"

    // You *do* need the return value of the join function, though.
    // output is not an array, however. It's a string.
    output.join(" "); // since this doesn't get saved to a variable, the result is thrown into the void
    return output;
}

I see what you’re trying, and you’re on the right track. It can be confusing to get your head around the concept of return values, but you’re close.


#5

Your original code was closest to the right answer. Your toUpperCase() method needs to have the ().

Also you will have to assign the return value of input[1].toUpperCase() to the array you just made.

So output[1] = output[1].toUpperCase() will capitalize the string in the second array index.

The next line will need a slight tweak as well.


#6

Thanks so much for your help. I really appreciate your taking the time to help out @br3ntor and @PortableStick. I’m still trying to wrap my head around JS algorithms. Starting small with some code challenges at Codewars and iokungfoo. It’s taking quite some time, but I am starting to get the hang of it. Here is what finally worked for me:

function kungfoo(input) {
    
    var output = input.split(" ");
        
        output[1] = output[1].toUpperCase();
        
        output = output.join(" ");
    
    return output;
}
/**
 * TEST CASES
 *
 * Test Case 1:
 *   input: "a b c"
 *  output: "a B c"
 *
 * Test Case 2:
 *   input: "one two three"
 *  output: "one TWO three"
 *
 * Test Case 3:
 *   input: "first second third"
 *  output: "first SECOND third"
 */