Комментарии:
Nice
ОтветитьVery helpful 👍
Ответитьfor the last question we can reduce the code like :
const obj = {}
array.forEach(item => {
obj[item] = obj[item]? obj[item] +1 : 1
})
console.log(obj)
I think there are better solutions for the first one, this was mine. It completes in constant time:
function rotate(arr, n){
return [...arr.slice(arr.length - (n % arr.length)), ...arr.slice(0, arr.length - (n % arr.length))];
}
Effectively it's grabbing the back and putting it on the front. It also account for values of n over arr.length using modular.
Super
Ответитьfor the last Question my code was
let arr = ["a", "b", "a", "c", "a", "b", "d"];
let obj= {};
for(let x of arr){
if( !obj[x]){
obj[x] =1;
}else{
obj[x]++;
}
}
console.log(obj);
for second question my approach was that..randomly
let str1 ="codehelpeee e";
let str2 = "decolhpee eee";
function compareStrCharacters(str1,str2){
for( let i=0; i<str1.length;i++){
char = str1[i];
index =str2.indexOf(char);
if(str2.includes(char)){
str2 = str2.slice(0,index)+str2.slice(index+1);
}else {
return false
}
}
return true;
}
console.log( compareStrCharacters(str1, str2));
I know but as you said I tried thank u.
To be very Genuine I followed the same approach for the first questions Mam.
ОтветитьQ3 !! optimised this way also.
const arr =['aaa','bbb','ccc','aaa','bbb'];
let obj = {};
arr.map((item) => {
obj[item] ? obj[item] +=1 : obj[item] = 1;
});
console.log(obj);
Very helpful. Thanks Huma.😍
Ответить👍
Ответить