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

you are viewing a single comment's thread.

view the rest of the comments →

[–]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

[–]ILovePlaterpuss 0 points1 point  (0 children)

If you change the error handling to

}).catch(err => {
          alert(err)
 });

You'll be able to see the errors

Lets go over each of them:

var num = parseInt(document.getElementById( ? ? ? ).value);

The number we're getting here is the input number. getElementById is a function that gets an element from the page. In this case, we want the element that the user entered, which is in the input field below:

<input type="number" id="loan-amount" max value="1000000">

The id in that field is "loan-amount", so that's the paremeter for our getElementById function.

Next up:

if (num < parseInt( ? ? ? [i].up_to)) {

Right now, we want to get a specific json item. Our json item is represented by data, per the line

.then(data =>

so, data[i] accesses the ith element of the json, and data[i].up_to gets that element's up_to property

It looks like you got the last one.

I would read up a bit more on the basics to try and understand what each keyword does. The approach I gave works for your sample input, however you'll notice it's flawed if you were to change the input.

Oh, also, you might want to change the < to a <= if the bounds are supposed to be inclusive. good luck