#Undefined and NaN stuck

12 messages · Page 1 of 1 (latest)

weary hull
#

So i am trying to get the input from the user straight as my arguments however, when i do that, it gives me an undefined and NaN error... but when i try to hard code the argument with a random number, it works perfectly. here is more of the code:

let input = document.getElementById("input")
const convertBtn = document.getElementById("convert-btn")
let length = document.getElementById("length")
const volume = document.getElementById("volume")
const mass = document.getElementById("mass")

convertBtn.addEventListener("click", function(){
forLength( 20)
forVolume()
forMass()

})

function forLength(input){

let toFeets = Math.floor((input * 3.281)*100)/100
let toMeters = Math.floor((input / 3.281)*100)/100


return length.innerHTML += `<p>${input} meters = ${toFeets} feets | ${input} feet = ${toMeters} meters</p>`

}
function forVolume(input){
let toGallon = Math.floor((input * 0.264)*100)/100
let toLiters = Math.floor((input / 0.264)*100)/100

return volume.innerHTML += `<p>${input} liters = ${toGallon} gallons | ${input} gallons = ${toLiters} liters</p>`

}
function forMass(input){
let toPounds = Math.floor((input * 2.204)*100)/100
let toKilos = Math.floor((input / 2.204)*100)/100

return mass.innerHTML += `<p>${input} kilos = ${toPounds} punds | ${input} punds = ${toKilos} kilos</p>`

}

plain harbor
#

In JavaScript, the value returned from an HTML <input> element is always a string, regardless of the input type (e.g., text, number, checkbox).

You have to explicitly convert it to number like

const inputValue = Number(input.value):

Then you can work with your inputValue as a number.

weary hull
plain harbor
#

My pleasure! Keep crushing that code! Hit me up if you need anything. 👊

weary hull
#

actualy yeah i do need some help, but i think it's more of a css isue and not JS.
I want the paragraphs of each conversions to stay within the divs and for the box to extend but i cant work out how to figure that out.

plain harbor
#

Maybe you fixed the height of the divs? Github repo would help me a lot to find the problem. blobnomcookie

weary hull
#

alright let me create a repo... will share it here just now

plain harbor
#

Ah, you always add the <p> element to the innerHTML. Insted just make it equal.

Change += to = on line 27 in your JS file.

#

Because you do it with multiple step, you need to change the length.innerHTML, the volume.innerHTML and the mass.innerHTML as well.

But, think about how could you make it more simple? Instead of writing the rendering logic multiple times, think about how you could solve it with one function, and maybe call it 3 times? - parameters, arguments, etc.

#

Maybe use variables to store values that you can reuse later in your code, without hard coding them.