#using a string in eval to display a value

103 messages ยท Page 1 of 1 (latest)

severe rose
#

it just returns undefined

var john_sheet = {
    name: 'John',
    weight: 92,
    height: 1.95,
    bmi: function() {
        return (this.weight / (this.height ** 2))
    }

}

var mark_sheet = {
    name: 'mark',
    weight: 78,
    height: 1.69,
    bmi: function() {
        return (this.weight / (this.height ** 2))

    }

}
var winner
var looser
if (john_sheet.bmi() < mark_sheet.bmi()) {
    winner = "mark_sheet"
    looser = "john_sheet"

} else {
    winner = "john_sheet"
    looser = "mark_sheet"
}
var list_win = [winner, looser]

for (var property in list_win) {
    console.log(`${property.name}'s BMI is ${property.bmi()} and his height is ${property.height}`)
}
gusty ore
jovial trench
#

#faq to learn modern web dev

#

If you know about const but write var, you were taught wrong. Also you somehow know **

severe rose
severe rose
ember sail
#

You can't use self.

waxen wasp
#

you aren't actually calling john_sheet.bmi

ember sail
#

but self.weight is gonna be undefined

jovial trench
#

If you know another programming language, go through impatient JS before you continue #faq

#

** is a "recent" addition to JS so I thought you were only taught modern JS and not other languages ๐Ÿ‘

severe rose
waxen wasp
#

LOL I didn't even notice the eval part of this. yikes...

jovial trench
#

You seem to come from python. Take JS conventions like camelCase and semi-colons now

#

Ideally you also lint your code #faq

#

And last but not least, eval is entirely useless here and should be avoided at all cost in any of your code

jovial trench
#

Among other things

ember sail
#

because there is no point

#

in using it

severe rose
#

yeah i get that now , i fixed it but now it can't access the sheet and it returns undefined when it runs ${property.name} in

    console.log(`${property.name}'s BMI is ${property.bmi()} and his height is ${property.height}`)
}```
jovial trench
#

Yes, don't do that

ember sail
#

because

#

you dont have an object

#

list_win is an array

jovial trench
#
const sheets = {
  john: {
    ... 
  }, 
  mark: {
    ... 
  }
};```
And then it's like in python with `[]`
#

And yes, please read through Impatient JS

#

You never need in in JS

severe rose
jovial trench
#

#faq

#

In the resources section

ember sail
#
var john_sheet = {
    name: 'John',
    weight: 92,
    height: 1.95,
    bmi: function () {
        return (this.weight / (this.height ** 2))
    }

}

var mark_sheet = {
    name: 'mark',
    weight: 78,
    height: 1.69,
    bmi: function () {
        return (this.weight / (this.height ** 2))

    }

}

var winner
var loser
if (john_sheet.bmi() < mark_sheet.bmi()) { // you didnt call the function
    winner = mark_sheet
    looser = john_sheet
} else {
    winner = john_sheet
    loser = mark_sheet
}
var list_win = [winner, looser]
console.log(list_win)

for(let i = 0; i < list_win.length; i++) {
    console.log(`${list_win[i].name}'s BMI is ${list_win[i].bmi()} and his height is ${list_win[i].height}`)
} // you use for like this to go thru an array or foreach
#

and since you set the winner as a string

#

that was also not possible

#

only if you would've had an object and then inside it you would have the sheets

gusty ore
jovial trench
#

I haven't seen good code where prototypal traversal is needed, and the confusion stemming from its use is too high

jovial trench
#

Objects inherits from other objects all the way to Object.prototype. in goes all the way up there to check for the property

gusty ore
# ember sail what's prototypal traversal?

It usually leads to complex and confusing code. This is especially true when it comes to traversing the prototype chain, which can become a labyrinthine maze of prototypes, properties, and methods.

ember sail
#

ah ic

gusty ore
severe rose
gusty ore
jovial trench
#

Some time ago, yes

#

Although I still open all messages from blocked people and react to it

ember sail
#

since when you can't react to people who you blocked

gusty ore
jovial trench
#

Blocked people cannot react

#

I can react to people I block

severe rose
#

also thank you so much guys , i got many good insights from this

#

i also understand that my teacher is a ***head for teaching me eval

ember sail
#

no

jovial trench
#

And var TBF. I don't know what's the worst ๐Ÿ˜

ember sail
#

eval is good in some situations

waxen wasp
#

no it's not

jovial trench
#

For a quick spin probably, but never in actual code

severe rose
#

python had it built in so i didn't really know about it

#

can i mark it as "solved"

severe rose
jovial trench
#

If you solved your issue with objects and there is no more in or var in your code, yes

jovial trench
#

#faq to share code on discord with syntax highlighting

severe rose
#

yes i saw it and i also used ` 3 times , but my code was not colored

ember sail
#

you need to put the lang

#

after the 3`

severe rose
ember sail
#

yes

#

but only if discord supports that lang

severe rose
#

ofc , but it is very useful

gusty ore
#

To get rid of the if statement

const johnSheet = {
  name: 'John',
  weight: 92,
  height: 1.95,
  bmi: function () {
    return this.weight / (this.height ** 2);
  },
};

const markSheet = {
  name: 'Mark',
  weight: 78,
  height: 1.69,
  bmi: function () {
    return this.weight / (this.height ** 2);
  },
};

const [winner, loser] = johnSheet.bmi() < markSheet.bmi()
  ? [markSheet, johnSheet]
  : [johnSheet, markSheet];

const listWin = [winner, loser];

listWin.forEach((person) => {
  console.log(`${person.name}'s BMI is ${person.bmi().toFixed(2)} and their height is ${person.height}`);
});
ember sail
#

it's kinda weird seeing "this" XD

gusty ore
ember sail
#

i've been using js for like 2 years

#

and i haven't seen anyone using this

waxen wasp
#

it's almost never needed

jovial trench
#

We also don't use .forEach ๐Ÿ˜‡

ember sail
#

i use foreach

jovial trench
#

Bad

ember sail
#

why

#

idk about speed difference

#

but it looks cleaner

#

no ;

jovial trench
ember sail
#

ok ok but

severe rose
#

ok ok this is too advanced for me xD

waxen wasp
#

search far enough back on this servers history and you'll see my noob ass attempting to justify forEach() usage. you'll eventually learn...

ember sail
#
const yes = [1,2,3,4,5,6,7,8]
let total = 0
yes.forEach((number) => {
  total += number
});```
#

looks smaller

gusty ore
#

Whether or not forEach is the best choice depends on the specific needs of the task you're trying to accomplish.

waxen wasp
#

for..of > forEach()

jovial trench
#

Also, reassigning const? Yeah, see why I tell you not to use it

ember sail
#

oh i didnt see

jovial trench
#

There is no good reason to use forEach over other HOFs or for-of if side effects

#

Also, for-of prepares you to the use of generators and for await-of. Win-win

gusty ore
#

yeah well forEach is designed specifically for side-effect operations, such as printing out values or updating properties, where the return value is not as important