简体   繁体   English

如何编写一个 function,它将字符串数组作为参数并打印出每个元素的第一个字母(每行一个)

[英]How to write a function that takes an Array of strings as an argument and prints the first letter of each element out (one per line)

I am working on my first javascript assignment and we have been asked to "Write a function that takes an Array of strings as an argument and prints the first letter of each element out (one per line)" however we must use a FOR OF loop, function, and charAt() the output should be HWTIMS each letter on its own line I am struggling because I only know how to complete the task by using the toString.charAt method but that is not what the assignment asked for.我正在处理我的第一个 javascript 作业,我们被要求“编写一个 function,它将字符串数组作为参数并打印出每个元素的第一个字母(每行一个)”但是我们必须使用 FOR OF 循环, function 和 charAt() output 应该是 HWTIMS 每个字母都在自己的行中 我正在苦苦挣扎,因为我只知道如何使用 toString.charAt 方法完成任务,但这不是作业要求的。 I also am not sure if the for of loop goes in the function or the function goes in the loop.我也不确定 for of 循环是否进入 function 或 function 进入循环。 Just started JS last week very confused.上周刚开始JS很迷茫。 Please help.请帮忙。

let arr = (["Hello", "World", "This", "Is", "My", "String"]);
//  required for of
for (let element of arr) {
    console.log(element.toString())
}
 



// required function
let myFunction = (element) => element.charAt(0);

 myFunction(arr)
 

// required charAt
var str = new String( "This is string" );

        
         console.log( arr.toString().charAt(0));
         console.log(  arr.toString().charAt(6));
         console.log(  arr.toString().charAt(12));
         console.log(  arr.toString().charAt(17));
         console.log(  arr.toString().charAt(20));
         console.log( arr.toString().charAt(23));

See below four examples, the first one is your answer, they are loops.看下面四个例子,第一个是你的答案,它们是循环。

 let arr = (["Hello", "World", "This", "Is", "My", "String"]); let myFunction = element => console.log(element.charAt(0)); console.log('\nfor of mode'); for (const iterator of arr) { myFunction(iterator); } console.log('\nfor each mode'); arr.forEach(element => { myFunction(element); }); console.log('\nfor each mode2'); arr.forEach( el => myFunction(el) ); console.log('\nfor mode'); for (let index = 0; index < arr.length; index++) { const element = arr[index]; myFunction(element); }

Use this expression on each loop:在每个循环上使用此表达式:

 // word is a string from the array of strings word.toString().charAt(0)+'\n'

Details are commented in example below详细信息在下面的示例中注释

 let hwtims = ["Hello", "World", "This", "Is", "My", "String"]; /* Declare new string outside of loop: let char = '' On each loop add to char: += Get the first character of each word: .toString().charAt(0) Add a newline: +'\n' */ const firstChar = array => { let char = ''; for (let word of array) { char += word.toString().charAt(0) + '\n'; } return char; }; console.log(firstChar(hwtims));

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何写出arr中的每个值,arr是一个字符串数组,每行一项 - How do I write out each of the values in arr, which is an array of strings, one item per line function 获取字符串列表并在矩形框中打印它们,每行一个 - function that takes a list of strings and prints them, one per line, in a rectangular frame 如何制作一个循环,打印出第一个字母,然后在新行上打印两个,等等 - javascript - How to make a loop that prints out first one letter, then two on a new line, etc - javascript 如何编写一个接受字符串并打印出该字符串的每个字母的JavaScript函数? - How to write a JavaScript function that takes a string and print out each letter of that string? 如何编写将第一个元素设置为数组的函数? - How to write a function that set first element to an array? 给定一个字符串数组,将每个字符串转换为: 如果第一个字母大写则大写 如果第一个字母小写则小写 - Given an array of strings, convert each string into: uppercase if first letter is capital lower case if first letter is small 使用JavaScript将每个数组元素的首字母大写 - Capitalize first letter of each array element using JavaScript 取出数组中每个元素的第一个和最后一个字母(javascript) - Take out first and last letter in every element in array(javascript) 按第一个字母对字符串数组进行分组 - Group array of strings by first letter 如何使用带箭头函数的 Array.map() 将数组中每个单词的第一个字母大写? - How to capitalize the first letter of each word in an array using Array.map() with an arrow function?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM