任务:
给出一个字符串数组,返回一个
Map<String, Integer>
,其中包含该数组中每个不同字符串的键,并且值是该字符串的长度。
我尝试了以下操作,但不满足给定的测试条件。
function wordLength(arr) {
let returnedMap = new Map();
let sorted=[];
for(let i = 0; i < arr.length; i++) {
sorted.push(arr[i].toLowerCase());
returnedMap.set(sorted[i], sorted[i].length));
}
return returnedMap;
}
测试条件:
test(`Expect Word length of ["a", "bb", "a", "bb"] to be Map{'a' : 1, 'b': 2}`, () => {
let returnedMap = new Map ([["a", 1], ["bb" , 2]])
expect(wordLength(["a", "bb", "a", "bb"])).toEqual(returnedMap);
})
test(`Expect Word length of ["aba", "cdda", "that", "this", "ddac"] to be Map { 'aba' => 3, 'cdda' => 4, 'that' => 4, 'this' => 4, 'ddac' => 4 }`, () => {
let returnedMap = new Map ([["aba", 3], ["cdda" , 4], ["that", 4], ["this", 4], ["ddac", 4]])
expect(wordLength(["aba", "cdda", "that", "this", "ddac"])).toEqual(returnedMap);
})
test(`Expect Word length of ["The", "quick", "brown", "fox", "jumped", "over", "the", "lazy", "dog"] to be Map {'the' => 3,'quick' => 5,'brown' => 5,'fox' => 3,'jumped' => 6,'over' => 4,'lazy' => 4,'dog' => 3 }`, () => {
let returnedMap = new Map ([['the', 3],['quick', 5],['brown', 5] ,['fox', 3],['jumped', 6],['over', 4],['lazy', 4],['dog', 3]])
expect(wordLength(["The", "quick", "brown", "fox", "jumped", "over", "the", "lazy", "dog"])).toEqual(returnedMap);
})