Week 03: Javascript, Arrays

Lecture

This lecture will focus on building on the basic javascript concepts we learned last week.

View the lecture

Homework

Reading

No new reading! Catch up or re-read the first two readings. Make sure you understand the basic concepts, come with questions next week if you don’t. The rest of this course will be based on these concepts.

Assignment

Part 1: Create a function to detect "almost anagrams". The function should take two strings and return true if the two strings contain exactly the same set of characters and false if not. e.g.

is_almost_anagram("care", "race")
// would return true

is_almost_anagram("tent", "teen")
// would return true (though these are not real anagrams)

is_almost_anagram("dog", "cat")
// return false

some_string.split('') and arr.includes(x) will be helpful.

Part 2: Using your function, write another function that takes an array of strings and returns true or false if they’re all anagrams of each other. Don’t rewrite the first function, use it. Hint, you don’t have to actually compare each combination, e.g. if it’s not an anagram of the first, it doesn’t matter if it’s an anagram of the third.

let arr = [‘some’, ‘strings’, ‘here’]
let res = are_all_almost_anagrams(arr)

Part 3: Extra Credit: Now write a is_anagram() function that only detects actual anagrams, i.e. is_anagram("tent", "teen") would return false but "care" and "race" would return true.

Send your work by creating a gist, jsbin or similar, or work locally and send me a zip file of all the files that make up your assignment (including an index.html to make it easy for me to run). Include some examples of your functions running. Send to zachary.schwartz@qc.cuny.edu by Thursday February 12th at 9am.

Resources