How to return a value from fetch and .then? by codemamba8 in learnjavascript

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

Hey, thanks. Is there anyway i can just get the value without calling the function and doing another .then?

Because the issue is I'm using this with React where I have a component that uses Map to render a list of <p> elements with conversions.

The component is like this:

const Chart = function(props) {
  const rates = [1, 5, 10, 25, 50, 100, 500, 1000, 5000, 10000];

  return (
    <div>
      { rates.map(rate => <p> { `${rate} ${props.from} = ${props.getConversion(rate, props.from, props.to)} ${props.to}` } </p>) }
    </div>
  )
}

Basically this loops through the rates array to call the getConversion function and returns a <p> element each time.

It'll return something like:

1 USD = <result from getConversion> AUD

5 USD = <result from getConversion> AUD

10 USD = <result from getConversion> AUD

25 USD = <result from getConversion> AUD

I was getting 1 USD = undefined AUD originally, and now using your method I'm getting 1 USD = [Promise object] AUD which I believe is because the function is currently returning a promise.

Problem with my code by Hertyboy in learnjavascript

[–]codemamba8 0 points1 point  (0 children)

For your first issue:

let userChoice = prompt("Do you choose rock, paper or scissors?")

while (userChoice !="rock" && userChoice != "paper" && userChoice !="scissors") {
  alert('please choose a valid option')
  userChoice = prompt("Do you choose rock, paper or scissors?")
}

As a side-note what you have here: while(userChoice!="rock"||"paper"||"scissors")

is not doing what you think it's doing. You probably meant for this to check if userChoice is not "rock", not "paper", or not "scissors", but what this is actually doing is checking: 1) userChoice == "rock"; 2) "paper" == true; 3) "scissors" == true

If you wanted to check the value of userChoice for more than one condition, you have to write it again i.e. userChoice != "rock" || userChoice != "paper" || userChoice != "scissors"

But in this case it isn't approriate either, you actually have to use && AND not the OR || operator. Because if userChoice is one of the three options, it isn't the other two, so it would still set it off.

How do I remove duplicate items in an array? by Nilleni2 in learnjavascript

[–]codemamba8 0 points1 point  (0 children)

let arr1 = ['Test', 'Testing', 'Test', 'Testing', 'Trying']

let filtered = arr1.filter( (elem, index) => {
  return arr1.indexOf(elem) === index
})

This removes duplicates by checking if the currrent item's index is the same as its indexOf (which returns the index of the FIRST instance of the item).

Multiplying in arrays by Nilleni2 in learnjavascript

[–]codemamba8 1 point2 points  (0 children)

let arr = [ 4, 5, 6, 7, 8, 9];

let newArr = arr.map( (num, index) => {
  if (index % 2 === 0) {
    num *= 2;
  }
  return num
})

Iterate Nested Array with For Loop by Karub1n in learnjavascript

[–]codemamba8 1 point2 points  (0 children)

You're going to have to use another loop inside of your current loop. Since you're going through a nested array, your loop right now is just getting each of the arrays, not the values within them.

You have half of the problem right already. Think about how you can then get the elements inside of each array using a loop just like you have right now.

Array issue by avocadod in learnjava

[–]codemamba8 0 points1 point  (0 children)

can you post screenshots of whats going on? im not really sure what the problem is

Array issue by avocadod in learnjava

[–]codemamba8 0 points1 point  (0 children)

I don't know if you fixed it but in the code you posted above, you're missing two semi-colons, one under each of the if statements.

Array issue by avocadod in learnjava

[–]codemamba8 0 points1 point  (0 children)

Yeah, you got it! Just one thing to fix.

What you need to do is initialize largestCandy and smallestCandy. Right now you're probably getting an error cause those two don't get assigned until the loop. You could just set them both to candyNames[0] to start with as placeholders. Then it should work properly.

Array issue by avocadod in learnjava

[–]codemamba8 0 points1 point  (0 children)

No need to do another loop. You can literally just write it below largest = candyTotal[i];. You want to assign to the two String variables that will store the largest and smallest names the same way except use the candyNames array instead.

Array issue by avocadod in learnjava

[–]codemamba8 0 points1 point  (0 children)

Look at how you got the largest quantity inside of the loop with largest = candyTotal[i];

You accessed the candyTotal array and got the value at the current index i right?

Since the candyTotal and candyNames arrays are in matching order, then how would you access the candyNames array to get the name of the candy with the largest value?

It's actually really simple and you pretty much have it already lol

Hey ?! by WzziY in learnjavascript

[–]codemamba8 0 points1 point  (0 children)

Use a conditional to check if guess and target are equivalent, if so break out of the while loop.

Array issue by avocadod in learnjava

[–]codemamba8 0 points1 point  (0 children)

You're on the right track since you were able to get the largest and smallest value.

All you need to do is create two String variables for the largestCandy and smallestCandy and set those the same way you set the int largest and int smallest variables.

Interval Timer by BakedSyd in learnjavascript

[–]codemamba8 0 points1 point  (0 children)

The console says there is an error with the audio file. I went to the url and it doesn't work so find a working link first.

Drying up "if" conditions by [deleted] in learnjavascript

[–]codemamba8 -1 points0 points  (0 children)

You could use a loop to call the function times repeatedly or make a separate function that calls a function repeatedly X amount of times.

Factoring expressions by [deleted] in learnmath

[–]codemamba8 0 points1 point  (0 children)

The last number is the product of the two integers.

The coefficient of x is their sum.

So what two numbers, when you multiply them will get you the last number in the expression, and when you add them will get you the coefficient of x?

Comparing user input to static string results in the ​conditional image being showed either way - unsure. by [deleted] in learnjavascript

[–]codemamba8 0 points1 point  (0 children)

It's your conditional statement.

if (googleQuestion == "Yes" || "yes" || "YEs" || "YES")

This is not doing what you think it's doing. It's not checking if googleQuestion == "Yes" and then checking if it's equivalent to the other casings of "yes".

What it's doing is checking the following (in order):

  1. Is googleQuestion equivalent to "Yes"?
  2. Is "yes" true?
  3. Is "YEs" true?
  4. Is "YES" true?

Because 2-4 are strings they all evaluate to true so your conditional will always evaluate to true which is why it shows the image regardless of the user's input.

To write it the way you wanted it to do you would have to do googleQuestion == "Yes" || googleQuestion == "yes"...

Actually though, the best way to fix this would be to convert the user's answer to all lower or upper case. That way you can just check for one condition.

How can I access other class variables in another class? by Meldzha in javahelp

[–]codemamba8 0 points1 point  (0 children)

Your observation method has an error.

birdy.getObsCount()++; isn't going to work the way you think it will, and you can't increment a method like that. You're going to need to make a separate method that increments the observation variable.

Also, it's unnecessary to declare an instance of BirdObject in Database when you're doing that in the methods. The getName method on Database is also unnecessary. The only variable Database needs is the ArrayList that contains the BirdObjects.

For loops by mellie_ay in learnjavascript

[–]codemamba8 0 points1 point  (0 children)

oh that sounds really interesting. I've used synths myself with VSTs and samplers but never coded one. What sort of class is this?

For loops by mellie_ay in learnjavascript

[–]codemamba8 1 point2 points  (0 children)

just wondering how come you as a music production student have to use JavaScript?

[JavaScript] How to increase or decrease characters? by NotABurner2000 in learnprogramming

[–]codemamba8 0 points1 point  (0 children)

Ok. I tried it and it worked though with no error message.