Last active
June 6, 2020 18:12
-
-
Save ashleytqy/6e5b991e4d15205f965b2727f0a1047a to your computer and use it in GitHub Desktop.
asey
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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