Possible typo in Javascript lesson

Lesson:

I believe there is a contradiction in the sentence following a code sample. If so, I’d be happy to put in a ticket.

This is the block of text/code I’m referencing – i highlighted the potentially-conflicting text in inline (backtick) code blocks, since it shows up better than bold or italics:

Once the object is frozen, you can no longer add, update, or delete properties from it. Any attempt at changing the object will be rejected without an error.

[there is a code sample here]

The obj.review and obj.newProp assignments will result in errors, and the console will display the value { name: “FreeCodeCamp”, review: “Awesome” }.

My test in replit.com seems to confirm there are no errors thrown, and that the object does not mutate.

The “will result in errors” text could mean that…there will be errors – mistakes (of logic) – in your code, but not javascript/es6/console errors actually get thrown.

But that would seem off.

Is there a typo here?

Looks quite error-y to me:

In vanilla JavaScript no error will be thrown, in 'strict' mode a TypeError will be thrown.

let obj = {
  name:"FreeCodeCamp",
  review:"Awesome"
};
Object.freeze(obj);

obj.name = 'will not change'
console.log(obj.name)

function useStrictMode () {
    'use strict';
    
    let obj = {
  name:"FreeCodeCamp",
  review:"Awesome"
};
Object.freeze(obj);

obj.name = 'will not change'
console.log(obj.name)
}

useStrictMode()
//logs global obj.name with no change made without throwing
VM546:8 FreeCodeCamp

//error thrown from calling the function as it was using strict mode
VM546:19 Uncaught TypeError: Cannot assign to read only property 'name' of object '#<Object>'
    at useStrictMode (<anonymous>:19:10)
    at <anonymous>:23:1
useStrictMode @ VM546:19
(anonymous) @ VM546:23

FCC seems to run their code in strict mode or something similar and it will throw an error in their test which is why they have:

try {
    MATH_CONSTANTS.PI = 99;
  } catch(ex) {
    console.log(ex);
  }

This will handle the error thrown and you in fact need this to be thrown to pass.

Commented out 'use strict':

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/freeze

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Strict_mode

1 Like

yep, my bad, i added the "use strict;" semi-directive to the replit scenario because i remembered i had a problem with an earlier javascript exercise, and at least part of the reason i was confused about the fCC evaluation of my code was because of that strict directive. forgot i left it in the replit example.

my post was in akismet purgatory, so couldn’t update.

i think we should make the fact that the fCC editor uses “use strict” more explicit – unless i just missed it somewhere. including a note about having to use it in your browser, replit, or any other evaluator you use – to write some test code.

so then the question is…is the text of the lesson correct? regardless of whether errors are thrown or not, the errors can’t be both thrown and not thrown, right?

I agree it could be worded better. I think they meant the two parts as separate and referring to different conditions, but this is not made clear, and it requires me to assume that was their meaning rather than it being explicit as it should be.

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.