Skip to content

Instantly share code, notes, and snippets.

@ashleytqy
Last active June 6, 2020 18:12
Show Gist options
  • Select an option

  • Save ashleytqy/6e5b991e4d15205f965b2727f0a1047a to your computer and use it in GitHub Desktop.

Select an option

Save ashleytqy/6e5b991e4d15205f965b2727f0a1047a to your computer and use it in GitHub Desktop.
asey
const memo = {};
const names = ['Alex', 'Charlie', 'Dolores', 'May', 'Alex', 'Claire', 'Dolores', 'Claire'];
for (let i = 0; i < names.length; i++)
{
let name = names[i];
memo[name] = i;
console.log(i, "\n", name, memo);
}
console.log("ans:\n" + memo.May);
console.log(memo["May"]);
// console.log(memo[May]);
////
const characters = ['Darth Vader', 'Princes Leia', 'Luke Skywalker', 'Han Solo'];
// the right way
// let longest = "";
// for (let i = 0; i < characters.length; i++){
// if (characters[i].length > longest.length)
// {
// longest = characters[i];
// }
// }
// your way is wrong because you initialize the variable longest inside the loop
// so for every i, it resets to ""
// and are unable to compare it with the other characters in the array
// rmb to initialize the variable that tracks whatever you are tracking before you do the loop
for (let i = 0; i < characters.length; i++){
let longest = "";
if (characters[i].length > longest.length)
{
longest = characters[i];
console.log(longest)
}
}
// https://repl.it/repls/BestRoundErrors#index.js
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment