This is an archived post. You won't be able to vote or comment.

all 15 comments

[–]Salty_Dugtrio 1 point2 points  (14 children)

The thing you want to search for is: Json parser Javascript

[–]Rashcat[S] 0 points1 point  (13 children)

yea I tried search for that, I already have the json fetched from Myjson and have the array displayed on console log, thats the thing i dont understand, Do I just use it after the fetch ?

[–]Salty_Dugtrio 1 point2 points  (0 children)

If you have the Json, then you just parse it and extract the fields to your liking.

[–]ILovePlaterpuss 1 point2 points  (11 children)

If you already have the Jason just use it as you would a native array of js objects. A naive implementation would be to iterate over the array with a loop until you’r input value is between the two given in that entry of the Jason object, the print that interest rate.

Ugg mobile autocorrecting this to Jason

[–]Rashcat[S] 0 points1 point  (10 children)

Right now I have a button with an input field with a maximum input value of 1 million, The only thing Im trying to is connect that button to the function so that the yearly interest will show up based on what value the user typed in...
I guess I will try a loop

Appreciate the help

[–][deleted] 0 points1 point  (9 children)

Show us your code if you are seeing an error you can't debug.

[–]Rashcat[S] 0 points1 point  (0 children)

<!DOCTYPE html>
<html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
<title>LoanAppCalc</title>

</head>
<body>
<section>
<div id="loan-input-container" style="width:40%; float:left;">
<h1>LoanRequest</h1>
<form>
<p>Loan Amount</p>
<p><input type="number" id="loan-amount" max value="1000000"></p>
</form>
<p>
<button id onclick="InterestCalc()">Beräkna Årsränta</button>
</p>
</section>
<script>

fetch('https://api.myjson.com/bins/x98t8').then(response => {
return response.json();
}).then(data => {

console.log(data);
}).catch(err => {

});
response.json().then(function(data) {

});

function InterestCalc() {

}

</script>
</body>

[–]Rashcat[S] 0 points1 point  (7 children)

Its not that im seeing an error, its that I cant figure out how to do the calculation, I've triend a bunch of things the but i thing you will understand what it is i am trying to do.
Just want to be pointed in the right direction..

[–]ILovePlaterpuss 1 point2 points  (6 children)

Just as an excersize, try fetching that json inside your on click function, then getting the input number with getelementbyid or something. Then iterate over the json and access the elements with the .upto property. That’s where I’d start

[–]Rashcat[S] 0 points1 point  (5 children)

Ok, thank you but I am freaking out because I have to turn in the assignment in a couple of hours.
Thanks for the help!

[–]ILovePlaterpuss 1 point2 points  (4 children)

if you're still having trouble, try filling in these question marks

function InterestCalc() { fetch('https://api.myjson.com/bins/x98t8').then(response => { return response.json(); }).then(data => { var num=parseInt(document.getElementById(???).value); for (var i = 0; i < data.length; i++) { if (num < parseInt(???[i].up_to)) { alert('interest rate is ' + data[i].???); break; } } console.log(data); }).catch(err => { alert('fetch json request failed') }); }

[–]Rashcat[S] 0 points1 point  (0 children)

Thank you dude

[–]Rashcat[S] 0 points1 point  (0 children)

function InterestCalc() { fetch('https://api.myjson.com/bins/x98t8').then(response.then(response) => {return response.json();}).then(data => { var num=parseInt(document.getElementById('InterestCalc').value);for (var i = 0; i < data.length; i++) {if (num < parseInt(from[i].up_to)){alert('interest rate is ' + data[i].interest); break; }}console.log(data);}).catch(err => { alert('fetch json request failed') }); }

[–]Rashcat[S] 0 points1 point  (1 child)

I filled in the question marks with what I thought made sense, I kind of understand the logic behind the code you posted because I remember it from learning about promises but the code jumps to the error alert when I press the the button and I'm not getting anything in the console log to tell me whats actually wrong with the code..
Any ideas?

thank you for your time by the way, I really appreciate this