\n\n\nThanks!","answerCount":14,"upvoteCount":1,"acceptedAnswer":{"@type":"Answer","text":"Hi Daniel Gomez !\n\nThis happens because the \"return value\" of window.prompt() i.e., your prompt box is a \"string\".\nSource: https://developer.mozilla.org/en-US/docs/Web/API/Window/prompt\n\nMystery: the if statement\nAs x = \"5\" and y = \"15\", the\nif(\"5\" > \"15\") // evaluates to true because in a string comparison, only the first characters are compared. So, \"5\" > \"1\" is true\n\nSolution:\nWrap your prompt function inside parseInt() like,\n\nvar x = parseInt(prompt(\"type x\"))\nvar y = parseInt(prompt(\"type y\"))","upvoteCount":5},"suggestedAnswer":[{"@type":"Answer","text":"Hello, Daniel Gomez !\nvar x = prompt(\"type x\") //5\nvar y = prompt(\"type y\") //15\n//15*5 = 75\ntest(x, y);\nThis happens as a result of multiplying the variable x by y.","upvoteCount":4},{"@type":"Answer","text":"Daniel Gomez I found the problem.\n\nThe input is being read and calculated as strings.\nSo 5 is greater than 15 as a string because they test the digit. And 5 is a greater digit than the 1 in the \"15\".\n\nIf you inputted 2 and 30 as it is, the results will seem to work true because 2 is less than the 3 in the \"30\".\nIn order for it to work as numbers, you need to convert it to an integer.\n\nReplace the x and y lines with:\nvar x = parseInt(prompt (\"type x\"));\nvar y = parseInt(prompt (\"type y\"));\n\nparseInt(string) will turn the string into numbers (IF the string only contains numbers. Eg: \"234\" will become 234, but \"2d4\" will maybe run an error or the variable will become NaN.\n\n\nAfter you replace that, it should work fine now. Hope this helps! ☺","upvoteCount":3},{"@type":"Answer","text":"As your inputs, if you do\n5\n15\nThen the output will be 3. 15/5\nBut if you do\n15\n5\nThen the output will be 74. 5*15","upvoteCount":2},{"@type":"Answer","text":"but when i type x 5 and y 15 the result is 75. And if I type x 15 and y 5 the result is 0.3333333333. I’m really confused","upvoteCount":2},{"@type":"Answer","text":"but if(5>15) is false","upvoteCount":1},{"@type":"Answer","text":"Daniel Gomez please provide code.","upvoteCount":1},{"@type":"Answer","text":"Donna [user id=\"9826932\"]Daniel Gomez[/user] so...\nif(5>15) {\n return 5 * 15; //75\n} else {\n return 15 / 5; //3\n}\nI am confused...if x is 5 and y is 15, that means a is 5 and b is 15.\nIn that case, a is LESS than b. Which means it should return b / a (15 / 5 = 3)?\nWhat am I missing?","upvoteCount":1},{"@type":"Answer","text":"https://code.sololearn.com/Wt2Z5dxDGIyv/?ref=app","upvoteCount":1},{"@type":"Answer","text":"test it please. i think that the problem is the prompt command, beacuse if i replace the variables with the direct values, the result is how you say","upvoteCount":1},{"@type":"Answer","text":"Alexander Sokolov x should be the 15 and y be the 5. Else the a > b (5 > 15) is false and the division is returned instead.","upvoteCount":0},{"@type":"Answer","text":"what is the order?","upvoteCount":0}]} }
+ 1

Why the result is 75?

Do you need type x =5 and y =15 <script> Var x = prompt (“type x”); Var y = prompt (“type y”); function test(a,b) { If(a>b){ return a*b; }else{ return b/a; } } alert(test(x,y)); </script> Thanks!

28th Jun 2018, 4:46 PM
Daniel Gomez
Daniel Gomez - avatar
14 Answers
+ 5
Hi Daniel Gomez ! This happens because the "return value" of window.prompt() i.e., your prompt box is a "string". Source: https://developer.mozilla.org/en-US/docs/Web/API/Window/prompt Mystery: the if statement As x = "5" and y = "15", the if("5" > "15") // evaluates to true because in a string comparison, only the first characters are compared. So, "5" > "1" is true Solution: Wrap your prompt function inside parseInt() like, var x = parseInt(prompt("type x")) var y = parseInt(prompt("type y"))
28th Jun 2018, 5:38 PM
777
777 - avatar
+ 4
Hello, Daniel Gomez ! var x = prompt("type x") //5 var y = prompt("type y") //15 //15*5 = 75 test(x, y); This happens as a result of multiplying the variable x by y.
28th Jun 2018, 4:50 PM
Alexander Sokolov
Alexander Sokolov - avatar
+ 3
Daniel Gomez I found the problem. The input is being read and calculated as strings. So 5 is greater than 15 as a string because they test the digit. And 5 is a greater digit than the 1 in the "15". If you inputted 2 and 30 as it is, the results will seem to work true because 2 is less than the 3 in the "30". In order for it to work as numbers, you need to convert it to an integer. Replace the x and y lines with: var x = parseInt(prompt ("type x")); var y = parseInt(prompt ("type y")); parseInt(string) will turn the string into numbers (IF the string only contains numbers. Eg: "234" will become 234, but "2d4" will maybe run an error or the variable will become NaN. After you replace that, it should work fine now. Hope this helps! ☺
28th Jun 2018, 5:38 PM
Andre Daniel
Andre Daniel - avatar
+ 2
As your inputs, if you do 5 15 Then the output will be 3. 15/5 But if you do 15 5 Then the output will be 74. 5*15
28th Jun 2018, 5:02 PM
Andre Daniel
Andre Daniel - avatar
+ 2
but when i type x 5 and y 15 the result is 75. And if I type x 15 and y 5 the result is 0.3333333333. I’m really confused
28th Jun 2018, 5:09 PM
Daniel Gomez
Daniel Gomez - avatar
+ 1
but if(5>15) is false
28th Jun 2018, 4:53 PM
Daniel Gomez
Daniel Gomez - avatar
+ 1
Daniel Gomez please provide code.
28th Jun 2018, 5:14 PM
Andre Daniel
Andre Daniel - avatar
+ 1
Donna Daniel Gomez so... if(5>15) { return 5 * 15; //75 } else { return 15 / 5; //3 } I am confused...if x is 5 and y is 15, that means a is 5 and b is 15. In that case, a is LESS than b. Which means it should return b / a (15 / 5 = 3)? What am I missing?
28th Jun 2018, 5:18 PM
Andre Daniel
Andre Daniel - avatar
28th Jun 2018, 5:27 PM
Daniel Gomez
Daniel Gomez - avatar
+ 1
test it please. i think that the problem is the prompt command, beacuse if i replace the variables with the direct values, the result is how you say
28th Jun 2018, 5:33 PM
Daniel Gomez
Daniel Gomez - avatar
0
Alexander Sokolov x should be the 15 and y be the 5. Else the a > b (5 > 15) is false and the division is returned instead.
28th Jun 2018, 4:54 PM
Andre Daniel
Andre Daniel - avatar
0
what is the order?
28th Jun 2018, 4:58 PM
Daniel Gomez
Daniel Gomez - avatar