Lil' Big Boy
Lil' Big Boy

Reputation: 21

Trouble replacing value of property in object

I'm having trouble resetting the contents of this 'super secret file'. Could somebody explain why I am getting the original contents back for my second console.log?


function getSecret(file, secretPassword) {
  file.opened = file.opened + 1;
  if (secretPassword == file.password) {
    return file.contents;
  } else {
    return "Password no likey. Go away.";
  }
}
function setSecret(file, secretPassword, secret) {
  if (secretPassword == file.passowrd) {
    file.opened = 0;
    file.contents = secret;
  }
}

var superSecretFile = {
  level: "classified",
  opened: 0,
  password: 2,
  contents: "Dr. Evil's isn't offering furlough."
};
var secret = getSecret(superSecretFile, 2);
console.log(secret);

setSecret(superSecretFile, 2, "Dr. Evil's is doubling staff wages.");
secret = getSecret(superSecretFile, 2);
console.log(secret);

Upvotes: 0

Views: 58

Answers (2)

Daimellah Sofiane
Daimellah Sofiane

Reputation: 113

You made a typo in the setSecret function :

function getSecret(file, secretPassword) {
  file.opened = file.opened + 1;
  if (secretPassword == file.password) {
    return file.contents;
  } else {
    return "Password no likey. Go away.";
  }
}
function setSecret(file, secretPassword, secret) {
/******************************************************************************/
    /**********HERE IS THE TYPO passowrd instead of password.***************/
/***************************************************************************/
  if (secretPassword == file.password) { 
    file.opened = 0;
    file.contents = secret;
  }
}

var superSecretFile = {
  level: "classified",
  opened: 0,
  password: 2,
  contents: "Dr. Evil's isn't offering furlough."
};
var secret = getSecret(superSecretFile, 2);
console.log(secret);

setSecret(superSecretFile, 2, "Dr. Evil's is doubling staff wages.");
secret = getSecret(superSecretFile, 2);
console.log(secret);

Upvotes: 2

Nacho Zullo
Nacho Zullo

Reputation: 571

You have a typo inside setSecret function.

if (secretPassword == file.passowrd) 

file.passowrd must be file.password

Upvotes: 2

Related Questions