+ 2

JavaScript - no output in if/else

Hey, I'm trying for longer than a hour now to fix the error. Why is my code incorrect? I need to output the string "vacation" if the input is == "August". This is my code. I tried it with the normal if (month = "August" but it didn't work, I tried it with ==, I even tried as you can see in the code now to convert it into a boolean and asked the code to give me the "vacation" text if the august var is the same like month.. I really don't know what's wrong here. Pls help me and explain where my mistake is. Thank you so much! var month = parseInt(readLine(), 10) var august = ("August"); if (august == month) { console.log("vacation"); }

8th Sep 2021, 1:03 PM
Deniz Lier
Deniz Lier - avatar
6 Respuestas
+ 5
Dennis Lier Don't parse input with int so remove parseInt from readLine() Just do this: var month = readLine(); if (month == 'August') { }
8th Sep 2021, 1:14 PM
A͢J
A͢J - avatar
+ 2
A͢J - S͟o͟l͟o͟H͟e͟l͟p͟e͟r͟ lol it worked thank you. But what was my mistake? The input line with "var month" was prepared by sololearn. How should I handle the problem without changing the first line?
8th Sep 2021, 1:17 PM
Deniz Lier
Deniz Lier - avatar
+ 1
Dennis Lier There is no problem in your code, problem is in readymade code provided by Sololearn because in test cases there is String but they parse String in int using parseInt which is wrong.
8th Sep 2021, 1:23 PM
A͢J
A͢J - avatar
+ 1
A͢J - S͟o͟l͟o͟H͟e͟l͟p͟e͟r͟ omg thank you so much you couldn't imagine how long I tried to understand why the code never worked. I thought I'm stupid
8th Sep 2021, 1:29 PM
Deniz Lier
Deniz Lier - avatar
+ 1
A͢J - S͟o͟l͟o͟H͟e͟l͟p͟e͟r͟ I never thought that the given line could be wrong so I just tripple checked my code and tried to change it haha
8th Sep 2021, 1:37 PM
Deniz Lier
Deniz Lier - avatar
0
Dennis Lier First time I was also confused but then I saw the first line.
8th Sep 2021, 1:30 PM
A͢J
A͢J - avatar