Mckennat33
Mckennat33

Reputation: 11

Accessing scopes from a function

I am having trouble converting this customerName variable to uppercase. I know that I am missing something small.

var customerName = 'bob'

 function upperCaseCustomerName() {
    customerName.toUpperCase();
    return customerName;
}

Upvotes: 0

Views: 497

Answers (5)

Curious Coder
Curious Coder

Reputation: 1

I am in the same bootcamp and also had trouble with this. I had initially wrote the code as follows:

var customerName = 'bob'

function upperCaseCustomerName(){
    return customerName.toUpperCase();
}

well, the test did not pass! I was on this for hours. My issue was misunderstanding what the test wanted --> "upperCaseCustomerName(): Write a function that accesses that global customerName variable, and uppercases it." You have more instruction if you open the indexTests.js file on vs code.

While my first code above will return the value BOB, that's not what the test is asking at all. The test requires you to MODIFY or REASSIGN the value of customerName to "BOB" so that it gives us the value of BOB.

function upperCaseCustomerName(){
    return customerName = customerName.toUpperCase();
}

Upvotes: 0

Mohammadreza
Mohammadreza

Reputation: 83

I'd suggest to use parameters here and be independent from outer scope of you function:

let customerName = "bob";

function upperCaseCustomerName(name) {
  return name.toUpperCase();
}

upperCaseCustomerName(customerName); // BOB

Upvotes: 0

Alias Cartellano
Alias Cartellano

Reputation: 364

"The toUpperCase () method does not change the original string" -w3Schools. You instead have to store it in a var as demonstrated below.

<body>
<span id="name"></span>
<script>
var customerName = 'bob'

 function upperCaseCustomerName() {
    var name=customerName.toUpperCase();//Here
    return name;
}
document.getElementById("name").innerText=upperCaseCustomerName();
</script>
</body>

Upvotes: 0

Vadzim
Vadzim

Reputation: 296

You need to return the transformed value

var customerName = 'bob'

function upperCaseCustomerName() {
    return customerName.toUpperCase();
}

upperCaseCustomerName() // 'BOB'

Upvotes: 0

Joshua Smart
Joshua Smart

Reputation: 206

Very easy mistake to make, the toUpperCase() function does not act in-place, meaning the result is returned, the correction would be:

var customerName = 'bob'

 function upperCaseCustomerName() {
    return customerName.toUpperCase();
}

Upvotes: 2

Related Questions