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!
\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}]} }
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!