NodeJS调试coderbyte任务



node.js在javascript文件中调试json数据时,你有一个程序在路由https://coderbyte.com/api/challenges/json/age-counting上执行get请求,该请求包含一个数据键,值是一个字符串,其中包含的项目格式为:key=string, age=integer。你的目标是以字符串格式打印出索引10到15之间的键id(例如q3kg6,mgqpf,tg2vm,…)。所提供的程序已经解析了数据并循环遍历每个项,但是您需要弄清楚如何修复它,以便它正确地只使用键id填充keyarray。下面是coderbyte给出的代码。

const https = require('https');
https.get('https://coderbyte.com/api/challenges/json/age-counting', (resp) => {
let content = '';
// push data character by character to "content" variable
resp.on('data', (c) => content += c);
// when finished reading all data, parse it for what we need now
resp.on('end', () => {
let jsonContent = JSON.parse(content);
let jsonContentArray = jsonContent.data.split(',');
let keyArray = [];
for (let i = 0; i < jsonContentArray.length; i++) {
  let keySplit = jsonContentArray[i].trim().split('=');
  keyArray.push(keySplit[1]);
}
console.log(keyArray.toString());
});
});

const https = require('https');
https.get('https://coderbyte.com/api/challenges/json/age-counting', (resp) => {
  let content = '';
  // push data character by character to "content" variable
  resp.on('data', (c) => content += c);
  // when finished reading all data, parse it for what we need now
  resp.on('end', () => {
    let jsonContent = JSON.parse(content);
    let jsonContentArray = jsonContent.data.split(',');
    let keyArray = [];
    console.log(jsonContentArray.length);
    // Traversing over the json content array and checking only for keys while ignoring the age.
    for (let i = 0; i < jsonContentArray.length; i+=2) {
      // extracting only the keys by triming the white space and splitting the data by "=" and taking the first character from the array.
      let key = jsonContentArray[i].trim().split('=')[1];
      // Now pushing only the 10*2 for getting the keys and ignoring the age. If you look into the json you get a better understanding.
// Here is an eg of json considered.
// data":"key=IAfpK, age=58, key=WNVdi, age=64, key=jp9zt, age=47, key=0Sr4C, age=68, key=CGEqo, age=76, key=IxKVQ, age=79, key=eD221, age=29, key=XZbHV, age=32, key=k1SN5, age=88, .....etc
      if (i>=20 && i< 30) {
        keyArray.push(key);
      }
    }
    console.log(keyArray);
  });
});

相关内容

  • 没有找到相关文章

最新更新