我正在使用JavaScript的框架p5.js构建天气预报站点。我对天气情况(例如几朵云)有一个描述,但问题是我使用的api仅具有小写字母描述。我不希望它们大写,但是我不知道如何将它们大写。有人可以帮我吗? (我尝试了许多香草JavaScript方法)
var weather;
var apiPath = 'http://api.openweathermap.org/data/2.5/weather?q=';
var apiKey = '&appid=e0342ddf94a760131ffacfa0e12bddf4';
var unit = '&units=metric';
var input;
function setup() {
noCanvas(270,125);
var button = select('#submit');
button.mousePressed(weatherAskAndDraw);
input = select('#city');
}
function CapFirst(string)
{
return string.charAt(0).toUpperCase() + string.slice(1);
}
function keyDraw(){
if (weather){
var temp = weather.main.temp;
var humidity = weather.main.humidity;
var minTemp = weather.main.temp_min;
var maxTemp = weather.main.temp_max;
var country = weather.sys.country;
var city = weather.name;
var vis = weather.visibility;
var des = weather.weather[0].description;
var windSpeed = weather.wind.speed;
fill(255);
CapFirst(des);
document.getElementById("p1").innerHTML = "City: " + city;
document.getElementById("p2").innerHTML = "Description: " + des;
document.getElementById("p3").innerHTML = "Temperature: " + temp;
document.getElementById("p4").innerHTML = "Humidity: " + humidity;
document.getElementById("p5").innerHTML = "Wind Speed: " + windSpeed;
}
}
function weatherAskAndDraw(){
var url = apiPath + input.value() + apiKey + unit;
loadJSON(url, gotData);
keyDraw();
}
function gotData(data){
weather = data;
}
答案 0 :(得分:1)
我没有和P5.js
合作,而是像js
一样简单
mode = "few clouds";
string = mode.charAt(0).toUpperCase() + mode.substr(1,mode.length).toLowerCase();
console.log(string);