-2

I am new to coding and I am having trouble getting the result I want from a simple If /Else statement.

When "Yes" is entered correctly as written It works but I would like the response to work regardles of case sensitivity (i.e yes, YES, yEs etc.)

let cheer = prompt('Did the Team win?');

if(cheer != 'Yes') {
    alert("We will win the next one!");
} else{
     alert("Lets Go Team");
}
M Appel
  • 9
  • 1
  • 5
    Have a look at [`.toLowerCase()`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/toLowerCase) and/or [`.toUpperCase()`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/toUpperCase). – gen_Eric Nov 04 '21 at 17:53
  • 2
    `window.prompt` returns a string. Whether or not you compare it to `Yes`, `yes` or whatever else is up to you. Strings in Javascript are case sensitive. The [`localeCompare`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/localeCompare) should be used to compare strings regardless of the casing. – Wiktor Zychla Nov 04 '21 at 17:56
  • `'Yes'` is case-sensitive as all strings are case-sensitive to most any computer language. Computers don't understand the context of human language. They do literally what you tell them. You'll have to do a case-insensitive comparison if that's what you want. – Brad Nov 04 '21 at 17:56
  • @WiktorZychla I was not aware of localeCompare, thanks for that! That will save me a lot of hassle. :-) – Brad Nov 04 '21 at 17:57

1 Answers1

3

try this:

let cheer = prompt('Did the Team win?');

if(cheer.toLowerCase() != 'yes') {
    alert("We will win the next one!");
} else{
    alert("Lets Go Team");
}
Maria Raynor
  • 342
  • 1
  • 9
  • Amazing! thank you so much. I was nervous to ask my first SO question but this was perfect and a fast response too! what would .toLowerCase() be called ? I tried reasearching some things for myself but I dont think i was searching the right key words – M Appel Nov 04 '21 at 17:55
  • @MAppel you're welcome. If this solves your problem, It is better to accept the answer so that others can use it. – Maria Raynor Nov 04 '21 at 17:58
  • @MAppel `.toLowerCase()` is one of the functions of a text string. – Maria Raynor Nov 04 '21 at 18:00
  • SO is telling me I can accept the answer in 5 mins and I surely will. Thank you for this additional advice! – M Appel Nov 04 '21 at 18:01