Olá,
Me considero iniciante em javascript, apesar de saber o básico.
Na parte 3.7 do livro, diz para excluir o código javascript do body e colocar na seção head. Porém ao reler a página o console acusa o erro de null ao buscar o total.innerHTML que foi identificado com o id total.
Não sei se compreendi mal ou falta de atenção em algum trecho, mas o head não é oque antecede o body, e por isso não seria possível ler o valor que estaria no DOM? Abaixo o meu código:
//////////////////////HTML
</head>
<body>
<table>
<tbody>
<tr>
<td>
<div>R$ 29,90</div>
</td>
<td>
<input type="number">
</td>
</tr>
</tbody>
<tr>
<td></td>
<td>Total da compra</td>
<td>
<div id="total">R$ 29,90</div>
</td>
<td></td>
</tr>
</table>
</body>
</html>
/////////////JAVASCRIPT
function moneyTextToFloat(text) {
var cleanText = text.replace(“R$”, “”).replace(",", “.”);
return parseFloat(cleanText);
}
function floatToMoneyText(value) {
var text = (value < 1 ? “0” : “”) + Math.floor(value * 100);
text = "R$ " + text;
return text.substr(0, text.length - 2) + “,” + text.substr(-2);
}
function readTotal() {
var total = document.getElementById(“total”);
return moneyTextToFloat(total.innerHTML);
}
function writeTotal(value) {
var total = document.getElementById(“total”);
total.innerHTML = floatToMoneyText(value);
}
console.log(readTotal());
console.log(writeTotal(123.45));
console.log(readTotal());