codemamba8

u/codemamba8


r/learnjavascript•

I have a function that fetches from an API. Then I'd like to assign a variable to a data point from the API.

Then at the end return this value. I'm using this function to run a loop that creates a chart of currency conversions.

This is the function:

  getConversion(amount, fromCurrency, toCurrency) {
    let converted; //want to return this variable at the end

    fetch(`${this.state.rootUrl}/latest?amount=${amount}&from=${fromCurrency}&to=${toCurrency}`)
      .then(response => {
          return response.json();
      })
      .then(data => {
        converted = data.rates[toCurrency] //assign the conversion amount
      })

    r
19


r/learnjavascript•

here is the exercise:

https://learn.freecodecamp.org/coding-interview-prep/data-structures/create-a-priority-queue-class/

Here is my solution so far:

function PriorityQueue () {
    this.collection = [];
    this.printCollection = function() {
      console.log(this.collection);
    };

    **problem seems to be with this method**
    this.enqueue = function(arr) {
      //if the arr's priority is less than any of the items priority, splice it in at that index
      for (let i = 0; i < this.collection.length; i++) {
            if (arr[1] < this.collection[i][1]) {
            this.collection.splice(i, 0, arr)
            break;
  
42

r/reactjs•

When should you store data in a component outside of state?

For example when would:

constructor(props) {
  super(props)
  this.state = {

  }

  this.info = {
    a: 'one',
    b: 'two',
    c: 'three',
  }

}

be preferable versus making info an object inside of state?

My current understanding is state should be used for data that's modified to render the UI while static data can be stored outside of it.

27

r/learnjava•

I'm trying to start the MOOC course which requires JDK and NetBeans.

Since I couldn't download JDK without an account I found out about OpenJDK and downloaded JDK SE 11 from here: http://jdk.java.net/11/

Then I made a folder in Program Files called 'Java' and pasted the unzipped folder there.

After, I went into System Variables and edited PATH to add the addresses for both the bin and lib folders.

Then I downloaded NetBeans and ran the installer, but got an error message saying "JDK was not found on this computer."

If it matters, I didn't have anything Java or NetBeans related on my comptuer beforehand.

Does anyone know how to fix this and get NetBeans going?

31

r/FreeCodeCamp•

I'm getting started on the Node challenges and am having trouble with Glitch. I've never used it before and have no idea how it works.

Here's the first challenge, printing a simple "hello world":

https://learn.freecodecamp.org/apis-and-microservices/basic-node-and-express/meet-the-node-console

So I opened the Glitch link FCC provides:

https://glitch.com/#!/import/github/freeCodeCamp/boilerplate-express/

And added console.log("Hello World") to server.js. Then I pasted the URL under 'Share' to the tester but just get "Hello World" is not in the server console.

Does anyone know how to fix this? Also, is it necessary to make a Glitch account?

EDIT: Found the solution. The Glitch lin

42

r/learnjavascript•

I'm running a forEach on an array. Inside of the forEach is a loop with a conditional.

At the end of each iteration, the values of the array should change so that the following iterations of the loop use the new values. The reason for this is so that each value can only be matched for once.

However, for some reason it's still using the original values even though when I console.log it, it shows up with the updated values.

Code:

function pairwise(arr, target) {
  let indexArr = [];

  arr.forEach( (num, index) => {
    for (let i=0; i < arr.length; i++) {
      if (num + arr[i] === target && i !== index) {
        indexArr.push(index);
        i
24

r/learnjavascript•

For a project I'm making, the user can click a button to save content. This appends anchor tags to a div.

Then, each time an anchor tag is clicked by the user, it will run a function to take them to that content. The same function is run each time but the argument passed in is different.

So for instance if there was:

<div>
    <a></a>
    <a></a>
    <a></a>
</div>

function getWeather(location)

Each <a> should run getWeather, but pass in a different location like getWeather('Los Angeles'), getWeather('Tokyo'), etc..

The first way I thought to do this was to use `.setAttribute('onclick', getWeather('L

27

r/learnjavascript•

I'm making a Pokemon-themed game and have an object in state that looks like this.

pokemonExp: {
         bulbasaur: {
          expPoints: 0,
         },
         squirtle: {
          expPoints: 0,
         },
         charmander: {
          expPoints: 0,
         },
      }

This object keeps track of the experience points of each Pokemon. Each time the player wins a battle, the object matching the Pokemon that was used should be updated. So if the player won a battle using Bulbasaur, then bulbasaur.expPoints should increment by 1.

I'm trying to figure out how to get the obj to update so that it not only updates the matching obj but also ret

35

r/learnjavascript•

I'm making a Pokemon-themed Hangman sort of game. In Pokemon, there are types (like elements) weak and strong to one another. For example, Water is strong against Fire, but weak against Grass.

That will be incorporated in my game by adjusting the amount of guesses the player gets depending on the type of theirs Pokemon versus the type of the Pokemon they're guessing.

So if the player is using a Water type and the unknown Pokemon is Grass type, then they'll have 5 guesses (weak against), but if the unknown Pokemon was a Fire type, they'd have 8 guesses (strong against).

The method I currently have is a function that checks the types and returns the effectiveness. But it seems very repeti

26



r/FreeCodeCamp•

https://codepen.io/codemamba/full/xeWyvw

I just got done with this calculator coded in vanilla JS and w/o using 'eval.'

This project was actually a lot harder than expected. It was fairly simple to get it to take inputs and output the answer, but it took a while to account for all the different cases such as: starting a new calculation after doing a previous one, continually operating on a previous result, or allowing for calculations of any length.

If you find something that's off or could be improved, please let me know. I just started learning JS last month so there's definitely a long way to go.

910


r/FreeCodeCamp•

I've finished the JavaScript section up to Intermediate Algorithms, which I've completed about 1/3rd of. I've also completed the Palindrome challenge for the JS projects.

But I'm having trouble finishing the rest, and am getting burnt out with algorithms.

So I'm thinking of moving on to the Front End projects and coming back to the JS section later. This way I can get experience building things and start learning DOM manipulation.

Do you think this is a good idea?

2312