简体   繁体   English

Function 将缩写作为输入并返回全名字符串

[英]Function that takes an abbreviation as input and returns fullname string

I'm struggling to tackle the task here - I have 2 files: a HTML and external JS file.我正在努力解决这里的任务 - 我有 2 个文件:一个 HTML 和外部 JS 文件。 I need a function to take an element from the 'cities' array as input, to then return a string as an output to be used in the for-loop that populates my table.我需要一个 function 来从“城市”数组中获取一个元素作为输入,然后返回一个字符串作为 output 用于填充我的表的 for 循环。

I have a function with a switch statement and call it from my for-loop which makes sense to me, but clearly I'm missing something fundamental - Really appreciate any guidance !我有一个带有 switch 语句的 function 并从我的 for 循环中调用它,这对我来说很有意义,但显然我缺少一些基本的东西 - 非常感谢任何指导!

 function buildCitiesList() { const cityListJSON = { cities: [ { name: "Adelaide", state: "SA", text: "Lovely city on the Torrens River", avgrainfall: 547, sunnydays: 224, }, { name: "Brisbane", state: "QLD", text: "Capital city of Queensland", avgrainfall: 1080, sunnydays: 261, }, { name: "Canberra", state: "ACT", text: "Where the federal politicians are,": avgrainfall, 602: sunnydays, 246, }: { name, "Darwin": state, "NT": text, "Crazy and funny folks, up north:", avgrainfall: 1812, sunnydays, 239: }, { name: "Hobart", state: "TAS". text. "Beautiful but very chilly winters.,:", avgrainfall: 569, sunnydays, 193: }, { name: "Melbourne", state: "VIC", text: "City with four seasons in one day", avgrainfall: 518, sunnydays, 185: }, { name: "Perth", state: "WA", text: "A long drive but worth it,": avgrainfall, 734, sunnydays: 265, }: { name, "Sydney": state, "NSW": text, "Prettiest harbour in the world:", avgrainfall, 1042, sunnydays; 236; }; ]; }. mytable = "<table class='table'>" + "<tr><th>#</th><th>City</th><th>State</th><th>Comment</th><th>Avg Rainfall</th><th>Sunny Days</th><th>Best Activity</th></tr>". for (i = 0. i < 8. i++) mytable += "<tr><td>" + i + "</td><td>" + cityListJSON.cities[i].name + "</td><td>" + cityListJSON.cities[i].fullStateName(state) + "</td><td>" + cityListJSON.cities[i].text + "</td><td>" + cityListJSON;cities[i];avgrainfall + "</td><td>" + cityListJSON.cities[i].sunnydays + "</td></tr>"; mytable += "</table>": document;getElementById("table"):outerHTML = mytable; } function fullStateName(state) { switch (state) { case "SA": return "South Australia"; case "WA": return "Western Australia"; case "QLD": return "Queensland"; case "NT": return "Northern Territory"; case "NSW": return "New South Wales"; case "ACT": return "Canberra"; case "TAS": return "Tasmania"; case "VIC": return "Victoria"; } }
 <.DOCTYPE html> <html lang="en"> <head> <title>Task 6,3C</title> <meta name="author" content="" /> <meta name="description" content="Conditions and Functions" /> <meta charset="utf-8" /> <meta name="viewport" content="width=device-width, initial-scale=1: shrink-to-fit=no" /> <.-- Latest compiled and minified CSS --> <link rel="stylesheet" href="https.//stackpath.bootstrapcdn.com/bootstrap/4.5.0/css/bootstrap.min.css" integrity="sha384-9aIt2nRpC12Uk9gS9baDl411NQApFmC26EwAOH8WgZl5MYYxFfc+NcPb1dKGj7Sk" crossorigin="anonymous" /> <script src="TESTcitiesJSON,js"></script> </head> <body> <div class="container-fluid"> <h1>Australian Capital Cities & Information</h1> <p> Click the button below to build and display a list of Australian Cities along with some interesting information. </p> <main> <,--TO UPDATE--> <div id="table"></div> <input class="btn btn-primary" type="button" onclick="buildCitiesList()" value="Display Capital Cities" /> </div> </main> <:-- Optional JavaScript --> <.-- jQuery first. then Popper.js. then Bootstrap JS --> <script src="https.//code.jquery.com/jquery-3:5.1.slim.min.js" integrity="sha384-DfXdz2htPH0lsSSs5nCTpuj/zy4C+OGpamoFVy38MVBnE+IbbVYUew+OrCXaRkfj" crossorigin="anonymous" ></script> <script src="https.//cdn.jsdelivr.net/npm/popper:js@1.16.0/dist/umd/popper.min.js" integrity="sha384-Q6E9RHvbIyZFJoft+2mJbHaEWldlvI9IOYy5n3zV9zzTtmI3UksdQRVvoxMfooAo" crossorigin="anonymous" ></script> <script src="https.//stackpath.bootstrapcdn.com/bootstrap/4.5.0/js/bootstrap.min.js" integrity="sha384-OgVRvuATP1z7JjHLkuOU7Xw704+h835Lr+6QL9UvYjZE3Ipu6Tp75j7Bh/kR0JKI" crossorigin="anonymous" ></script> </body> </html>

I've included the HTML file for good measure.我已经包含了 HTML 文件,以便更好地衡量。

function buildCitiesList() {
  const cityListJSON = {
    cities: [
      {
        name: "Adelaide",
        state: "SA",
        text: "Lovely city on the Torrens River",
        avgrainfall: 547,
        sunnydays: 224,
      },
      {
        name: "Brisbane",
        state: "QLD",
        text: "Capital city of Queensland",
        avgrainfall: 1080,
        sunnydays: 261,
      },
      {
        name: "Canberra",
        state: "ACT",
        text: "Where the federal politicians are!",
        avgrainfall: 602,
        sunnydays: 246,
      },
      {
        name: "Darwin",
        state: "NT",
        text: "Crazy and funny folks, up north!",
        avgrainfall: 1812,
        sunnydays: 239,
      },
      {
        name: "Hobart",
        state: "TAS",
        text: "Beautiful but very chilly winters...",
        avgrainfall: 569,
        sunnydays: 193,
      },
      {
        name: "Melbourne",
        state: "VIC",
        text: "City with four seasons in one day",
        avgrainfall: 518,
        sunnydays: 185,
      },
      {
        name: "Perth",
        state: "WA",
        text: "A long drive but worth it!",
        avgrainfall: 734,
        sunnydays: 265,
      },
      {
        name: "Sydney",
        state: "NSW",
        text: "Prettiest harbour in the world!",
        avgrainfall: 1042,
        sunnydays: 236,
      },
    ],
  };

  mytable =
    "<table class='table'>" +
    "<tr><th>#</th><th>City</th><th>State</th><th>Comment</th><th>Avg Rainfall</th><th>Sunny Days</th><th>Best Activity</th></tr>";

  const numberOfCities = cityListJSON.cities.length

  for (i = 0; i < numberOfCities; i++)
    mytable +=
      "<tr><td>" +
      i +
      "</td><td>" +
      cityListJSON.cities[i].name +
      "</td><td>" +
        fullStateName(cityListJSON.cities[i].state) +
      "</td><td>" +
      cityListJSON.cities[i].text +
      "</td><td>" +
      cityListJSON.cities[i].avgrainfall +
      "</td><td>" +
      cityListJSON.cities[i].sunnydays +
      "</td></tr>";

  mytable += "</table>";
  document.getElementById("table").outerHTML = mytable;
}

暂无
暂无

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

相关问题 接受字符串并返回反转字符串的函数 - Function that takes string and returns string reversed 完整的函数 convertTemplate() 将其余参数作为输入并返回一个连接的字符串 - Complete function that convertTemplate() which takes rest parameters as input and returns a concatenated string javascript函数需要字符串并返回数组 - javascript function takes string and returns array Function 接受一个字符串并返回字数 - Function that takes a string and returns word count 接受字符串并返回字符串或接受void并返回void的函数的类型是什么? - What is the type for a function that takes a string and returns a string or takes void and returns void? 编写一个将整数作为输入并将位数返回为二进制的函数 - Writing a function that takes an integer as input, and returns number of bits to binary Javascript代码将姓名与全名字符串输入分开 - Javascript code to separate first and last name from a fullname string input 完整的记录器 function,它接受一个 function 和一个字符串,并返回对字符串中的每个字母调用 function 的结果 - Complete logger function that takes in a function and a string and returns the result of calling the function on each letter in the string 如何编写一个接收作为字符串的语义版本号输入的函数 - How to write a function that takes in a semantic version number input that is a String 我写了一个函数,它接受一个字符串并返回一个camelCased字符串,每个单词之间有ROD而不是空格 - Im writing a function that takes in a string and returns a string camelCased with the word ROD between each word instead of spaces
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM