我必须为课程分配创建一个Scrabble Scorer。在此程序中,我必须创建一个使用现有对象“ oldScoreKey”的函数,并创建一个newScoreKey,其中26个字母为键,并且为小写字母。每个字母的点就是值。听起来很简单,但是我在完成这部分工作时仍然遇到问题。我希望我可以输入一个新的分数键,但是此分配的要求是要为我做一个函数。我不确定我要执行的功能缺少什么。任何建议或解决方案的解释将不胜感激。我希望我所说的是有道理的,在此先感谢。
在过去的一周中,我尝试了forEach()和for ... in循环语句。对于小写字母,我使用了.toLowerCase,但是一直出现语法错误,或者它被忽略了。
1: ["A", "E", "I", "O", "U", "L", "N", "R", "S", "T"],
2: ["D", "G"],
3: ["B", "C", "M", "P"],
4: ["F", "H", "V", "W", "Y"],
5: ["K"],
8: ["J", "X"],
10: ["Q", "Z"]
}
//My most recent attempt
function transform(oldScoreKey){
for(let key in oldScoreKey){
for(let i = 0; i < oldScoreKey[key].length; i++){
for(let items in oldScoreKey[key][i]){
let newScoreKey =+ oldScoreKey[items][keys];
newScoreKey.toLowerCase();
return newScoreKey;
}
}
}
}
//CLOSE BUT NOT RIGTH: SHows each letter on it's own line but the point value is on it's own line as well right below it.
function transform(oldScoreKey){
for(key in oldScoreKey){
for(let i = 0; i < oldScoreKey[key].length; i++){
let newScoreKey = {};
newScoreKey += oldScoreKey[key][i];
return newScoreKey[key];
}
}
}
My expected results when returning newScoreKey is a new object which shows all 26 letters in lowercase as the keys and their point value as the vale.
Ex) a : 1,
e : 1,
and so on
Instead I'm getting undefined or all the letters each with their point values listed under them in the console screen.
答案 0 :(得分:0)
在函数内部创建一个新对象,并在每次内部迭代时,将其分配给该对象的属性,然后在函数的最后返回该对象:
const obj = {
1: ["A", "E", "I", "O", "U", "L", "N", "R", "S", "T"],
2: ["D", "G"],
3: ["B", "C", "M", "P"],
4: ["F", "H", "V", "W", "Y"],
5: ["K"],
8: ["J", "X"],
10: ["Q", "Z"]
}
function transform(oldScoreKey){
const newObj = {};
for (const [letterValue, letterArr] of Object.entries(obj)) {
for (const letter of letterArr) {
newObj[letter.toLowerCase()] = letterValue;
}
}
return newObj;
}
console.log(transform(obj));
答案 1 :(得分:0)
一种方法是获取输入键(使用Object.keys
)并在这些键上循环。对于每个键(score
),然后遍历每个字母。将该字母分配给具有score
值的新对象。返回新创建的对象。
这是这种方法:
const start = {
1: ["A", "E", "I", "O", "U", "L", "N", "R", "S", "T"],
2: ["D", "G"],
3: ["B", "C", "M", "P"],
4: ["F", "H", "V", "W", "Y"],
5: ["K"],
8: ["J", "X"],
10: ["Q", "Z"]
};
function transform(oldScoreKey) {
const result = {};
Object.keys(oldScoreKey).forEach(score => {
oldScoreKey[score].forEach(letter => {
result[letter] = score;
});
});
return result;
}
console.log(transform(start));
答案 2 :(得分:0)
<com.google.android.material.bottomnavigation.BottomNavigationView
android:id="@+id/bottomNavigationView"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:labelVisibilityMode="unlabeled"
app:elevation="0dp"
app:menu="@menu/bottom_navigation">
</com.google.android.material.bottomnavigation.BottomNavigationView>
答案 3 :(得分:0)
有几种方法可以做到这一点;一种方法是通过create or replace function consolidated_ad_traffic()
returns table( traffic_source text
, ad_month timestamp with time zone
, ad_cost numeric(11,2)
, ad_sales numeric(11,2)
, conversion_cost numeric(11,6)
)
language sql
AS $$
with ad_sources as
( select 'Google' as traffic_source
, "date" as ad_date
, round(cast (cost AS numeric ) / 1000000.0,2) as cost
, sales
, cost_per_conversion
from googleads_campaign
union all
select 'Taboola'
, "date"
, spent
, sales
, cost_per_conversion
from taboola_campaign
union all
select 'Microsoft'
, "TimePeriod"
, "Spend"
, sales
, cost_per_conversion
from microsoft_campaign
)
select * from ad_sources;
$$;
函数,如下所示:
select * from consolidated_ad_traffic();
select distinct on( traffic_source, to_char(ad_month, 'mm'))
traffic_source
, to_char(ad_month, 'Mon') "For Month"
, to_char(sum(ad_cost) over(partition by traffic_source, to_char(ad_month, 'Mon')), 'FM99,999,999,990.00') monthly_traffic_cost
, to_char(sum(ad_cost) over(partition by traffic_source), 'FM99,999,999,990.00') total_traffic_cost
from consolidated_ad_traffic();
select traffic_source, sum(ad_cost) ad_cost
from consolidated_ad_traffic()
group by traffic_source
order by traffic_source;
select traffic_source
, to_char(ad_month, 'dd-Mon') "For Month"
, sum(ad_cost) "Monthly Cost"
from consolidated_ad_traffic()
where date_trunc('month',ad_month) = date_trunc('month', date '2019-07-01')
and traffic_source = 'Google'
group by traffic_source, to_char(ad_month, 'dd-Mon') ;
答案 4 :(得分:0)
您可以简单地做到这一点
function transform(ob){
var newObject = {};
Object.keys(ob).forEach((key)=>{
ob[key].forEach((character)=>{
newObject[character] = key;
})
})
return newObject;
}