-3

If a number under 96 is entered than 0 should appear, between 96 and 192 3 should appear, and if the input is equal to or greater than 192 the output should be 6. however in my code for some reason that is not the case, inputs 0 and 1 return 0, input 2-99 returns 6, 100 through 191 returns 0, and 192 and above return 6

here is the code

var number = document.getElementById("width").value;
var text;


if (number >= "192") {
    text = "6";


} else if (number >= "96") {
    text = "3";

} else {
    text = "0";
}
document.getElementById("smallquantity").innerHTML = text;
  • 1
    What is the user entering into `#width`? Will it be a number, or a letter? – BenM Dec 10 '16 at 19:43
  • 3
    Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, it’s hard to tell exactly what you're asking. – Zakaria Acharki Dec 10 '16 at 19:45
  • 1
    Do you understand how string comparison works? Take a look at [this question](http://stackoverflow.com/questions/10863092/why-is-string-11-less-than-string-3). The string `"192"` and `"c"` are not the same thing. – Spencer Wieczorek Dec 10 '16 at 19:46
  • 1
    Strings ain't Numbers. check this: `console.log("5 is bigger than 10000: ", "5" > "10000")` – Thomas Dec 10 '16 at 19:47
  • It seems you try to, but you can't directly compare character vs number. try parseInt() or charCodeAt() methods – Banzay Dec 10 '16 at 19:52
  • @benM the user will be entering a number, not a letter – traktorgenius Dec 10 '16 at 20:08

5 Answers5

0

Try compare Strings

var letter = document.getElementById("width").value;
var text;

// If the letter is "c"
if (letter.localeCompare("c")) {
    text = "6";

// If the letter is "e"
} else if (letter.localeCompare("e")) {
    text = "3";

// If the letter is anything else
} else {
    text = "0";
}
document.getElementById("smallquantity").innerHTML = text;
albertoiNET
  • 1,280
  • 25
  • 34
0

I think you have tried your if logic wrong way. Try following

if(letter < "96")
   text = "0";
else if (letter < "192")
   text = "3";
else
   text = "6";

i think it will give the output you are looking for. But i would strongly suggest you to parseint the input then use it in the if block with number comparison.

var letter = parseInt(document.getElementById("width").value);
if(letter < 96)
   text = "0";
else if (letter < 192)
   text = "3";
else
   text = "6";
reza
  • 1,507
  • 2
  • 12
  • 17
  • I think that his whole approach is wrong. `letter < "192"` is incorrect in so many levels. – Mike B Dec 10 '16 at 20:03
  • might be. but we can not be sure because we don't know what his input is. from his description what i get he is getting output but not correctly because of his wrong logic design in if block. – reza Dec 10 '16 at 20:08
  • and he also said *the user will be entering a number, not a letter* in the last comment – reza Dec 10 '16 at 20:09
0

Strings are compared lexigraphically, meaning based on the ordering of the characters when listing things "alphabetically"; comparing two strings that happen to contain numbers does not do the same thing as comparing two numbers.

"2" > "1294" because the character 2 comes after the character 1.

Simply change your logic to use numbers. First of all, parse the contents of the HTML element. The 10 below means use base 10 when interpreting the number (important because something like "012" is interpreted as an octal number, not decimal):

var letter = parseInt(document.getElementById("width").value, 10);

...then change your comparisons to use numbers, not strings:

if (letter >= 192)
Jacob
  • 77,566
  • 24
  • 149
  • 228
0

To compare that with ASCII values, you first need to convert it to ASCII. You cannot compare right away.
Also, letter "c" has ASCII value of 99, not 192.
You should convert to ASCII first and then compare with correct values (numeric values, not number as string).
Also your logic is wrong - IF is "c" --> ELSE is "c" or "e". You will never get that "c" in ELSE.

var letter = document.getElementById("width").value.charCodeAt(0);
var text;

// If the letter is "c"
if (letter ==  99) {
    text = "6";

// If the letter "e"
} else if (letter == 101) {
    text = "3";

// If the letter is anything else
} else {
    text = "0";
}
document.getElementById("smallquantity").innerHTML = text;

Now you understand the idea. You can adjust the IFs accordingly.

Community
  • 1
  • 1
Mike B
  • 2,756
  • 2
  • 16
  • 28
0

After taking help from @jacob and @reza this is the code that i came up with that solved my problem.thank you to all those who helped me figure this one out!

    var number = parseInt(document.getElementById("width").value, 10);
    var text;
     if (number < "96") {
    text = parseFloat(0);

    } else if (number < "192") {
    text = parseFloat(3);

    } else {
    text = parseFloat(6);
    }
    document.getElementById("largequantity").innerHTML = parseFloat(text) * parseFloat(quantity);