Click here to Skip to main content
15,887,027 members
Please Sign up or sign in to vote.
1.00/5 (3 votes)
See more:
JavaScript
const buttonKonvertuj = document.getElementById("konvertuj")

function konverzija() {
	
	const inputVrednost = document.getElementById("vrednost")
	const inputValutaIz = document.getElementById("valutaIz")
	const inputValutaU = document.getElementById("valutaU")
	
	const v = Number(inputVrednost.value)
	const valIz = inputValutaIz.value
	const valU = inputValutaU.value
	 
	const valute = {
		"eur": 117.3201,
		"rsd": 1,
		"cad": 81.2437,
		"usd": 110.5420
	}
	
	const k = v * valute[valIz] / valute[valU]
	window.alert( valIz.toUpperCase() + "u" + valU.toUpperCase() +
                       ":" + k	)
	}	
}

buttonKonvertuj.onclick = konverzija


What I have tried:

JavaScript
const buttonKonvertuj = document.getElementById("konvertuj")

function konverzija() {
	
	const inputVrednost = document.getElementById("vrednost")
	const inputValutaIz = document.getElementById("valutaIz")
	const inputValutaU = document.getElementById("valutaU")
	
	const v = Number(inputVrednost.value)
	const valIz = inputValutaIz.value
	const valU = inputValutaU.value
	 
	const valute = {
		"eur": 117.3201,
		"rsd": 1,
		"cad": 81.2437,
		"usd": 110.5420
	}
	
	const k = v * valute[valIz] / valute[valU]
	window.alert( valIz.toUpperCase() + "u" + valU.toUpperCase() +
                       ":" + k	)
	}	
}

buttonKonvertuj.onclick = konverzija
Posted
Updated 24-Oct-23 11:01am
v2
Comments
OriginalGriff 8-Oct-23 5:36am    
"It doesn't work" is probably the most useless problem report we get - and we get it a lot. It tells us nothing about what is happening, or when it happens.
So tell us what it is doing that you didn't expect, or not doing that you did.
Tell us what you did to get it to happen.
Tell us any error messages.
Use the "Improve question" widget to edit your question and provide better information.
M Imran Ansari 8-Oct-23 13:56pm    
Kindly share the complete code with context so that we can help out.

1 solution

It is very difficult to know what the problem is with the explanation you give.

First of all: each statement should be ended by a ;

const buttonKonvertuj = document.getElementById("konvertuj");

function konverzija() {
	
	const inputVrednost = document.getElementById("vrednost");
	const inputValutaIz = document.getElementById("valutaIz");
	const inputValutaU = document.getElementById("valutaU");

and so on...



If that doesn't work, try something like:

function konverzija() {

alert("Entering the body of the function);
	const inputVrednost = document.getElementById("vrednost");
alert("Before inputVrednost");
	const inputValutaIz = document.getElementById("valutaIz")
alert("Before inputValutaU");
	const inputValutaU = document.getElementById("valutaU");
alert("Before v");	
	const v = Number(inputVrednost.value);

and so on...

That way you will be able to know which instruction really has the problem.
 
Share this answer
 
Comments
Richard Deeming 23-Oct-23 9:01am    
The OP didn't provide anywhere near enough information to know what the problem actually is. But we do know it's not the missing semicolons - thanks to automatic semicolon insertion[^], JavaScript doesn't really require a statement terminator most of the time. So whilst it's always better to explicitly terminate your statements, not doing so wouldn't cause the script to stop working.

As for your second code block, you're missing the closing quotes on your strings. Those will cause JavaScript errors, and prevent your code from working at all.

And using alert for debugging is a terrible idea anyway. If for some reason you can't use the JavaScript debugger that's built into every modern web browser, you should at least be using console.debug rather than alert. It'll be less annoying, and less likely to interfere with the code you're trying to debug.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900