Getting random images from unsplash api? [closed] - javascript

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 3 years ago.
Improve this question
I'm new to working with API's and coding in general (started 2018) and I'm currently working with the Unsplash API. The problem is when I search for a term like "mountains", the API returns random images that have nothing to do with my search term. For example when I search "mountains" I'm getting pictures of motorbikes?! Help please! :D
Here's the HTML:
<!DOCTYPE html>
<html lang="en">
<head>
<title>API-Project</title>
</head>
<body>
<input type="text" id="search" />
<button id="searchButton">Search!</button>
<p id="result"></p>
<script src="/app.js"></script>
</body>
</html>
And the js:
document.getElementById("searchButton").addEventListener("click", function() {
let accessKey = "my_access_key";
let searchTerm = document.getElementById("search").innerHTML;
//let nextPage = 1;
let url =
"https://api.unsplash.com/photos/?client_id=" +
accessKey +
"&query=" +
searchTerm +
"&page=" +
nextPage +
"&per_page=20";
// Request
fetch(url)
.then(function(data) {
return data.json();
})
.then(function(data) {
console.log(data);
document.getElementById("result").innerHTML = JSON.stringify(data);
});
});

Just like #Turnip said, if the element with id: 'search' is a form input element like;
<input type='text'/>
<textarea></textarea>
you should replace your line 3 this instead:
let searchTerm = document.getElementById("search").value;
just checked the API you using, seems the way you calling it is different from the way it's documented:
https://api.unsplash.com/search/photos?client_id=YOUR_ACCESS_KEY?page=1&query=mountains

Related

EmailJS message can't be sent [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 23 hours ago.
Improve this question
I'm new to EmailJS and trying to use it on my portfolio in the contact form. I tried different solutions and still showing the same error, I think I'm stuck now
I created the account, activated it, activated MFA as well, and tried multiple codes from the doc of EmailJS and from tutorials and still showing me the same error on the console. I even changed the service_id, template_id, and public key.
The error: Uncaught The public key is required. Visit https://dashboard.emailjs.com/admin/account
code on HTML:
<script type="text/javascript"
src="https://cdn.jsdelivr.net/npm/#emailjs/browser#3/dist/email.min.js">
</script>
<script type="text/javascript">
(function(){
emailjs.init("My-Public-Key");
});
</script>
code on JS:
function SendMail(){ event.preventDefault();
var params ={
from_name : document.getElementById("fullname").value,
email_id : document.getElementById("email-id").value,
message : document.getElementById("message").value
}
const serviceID = "service_uzy3k87";
const templateID = "template_btcihm8";
emailjs.send(serviceID, templateID, params)
.then(res=>{
document.getElementById("fullname").value = "";
document.getElementById("email-id").value = "";
document.getElementById("message").value = "";
console.log(res);
alert("Your message sent successfully!!")
})
.catch(err=>console.log(err));
}

If else displaying wrong answer when using fetch [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 1 year ago.
Improve this question
So what I'm trying to do is create an if/else statement, where if your IP is from Japan the console log will show "You are in Japan", and if your IP is from any other country the console log will show "I guess you are in the USA". The only problem is, it doesn't work. Even when I use VPN to pretend I am from Japan, the console log still displays "I guess you are in the USA".
This is the code I currently have:
async function country() {
let response = await fetch("https://ipinfo.io/json?token=8a21f471f4e583")
let CountryResponse = await response.json()
console.log(CountryResponse)
if (CountryResponse == "JP") {
console.log('You are in Japan');
} else {
console.log('I guess you are in the USA');
}
}
country()
So basically I would just like for the console log to show "You are in Japan" if the IP is from Japan, anything else the console log can show "I guess you are in the USA".
if (CountryResponse.country === "JP") {
console.log('You are in Japan');
} else {
console.log('I guess you are in the USA');
}
The api you are calling does not return only the county code but a object witch contains this information

Javascript Discord Bot 'SyntaxError: missing ) after argument list' [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 2 years ago.
Improve this question
I've been trying to create a discord bot that filters certain words and then sends a message afterwards. Here is my current code:
const Discord = require('discord.js');
const client = new Discord.Client();
client.on('ready', () => {
console.log(`Logged in as ${client.user.tag}!`);
});
//runs the message looker thingy
client.on('message', async message function {
//1 blacklisted words
let blacklisted = ['communism', 'fascism', 'socialism', 'conservatism', 'racism', 'sexism', 'nazism', 'marxism'] //words put , after the word
//2 looking for words
let foundInText = false;
for (var i in blacklisted) { // loops through the blacklisted list
if (message.content.toLowerCase().includes(blacklisted[i].toLowerCase())) foundInText = true;
}
// checks casesensitive words
//3 deletes and send message
if (foundInText) {
message.delete();
client.message.send('ISM SPOTTED. OFFICIAL ISM REPORT HAS BEEN FILED AND WILL BE OVERLOOKED BY THE HIGH ISM COUNCIL. ANYTHING YOU SAY CAN AND WILL BE USED AGAINST YOU IN A COURT OF LAW. YOUR FATE SHALL BE DETERMINED.');
}
});
However I keep experiencing an error saying:
client.on('message', async message function {
^^^^^
SyntaxError: missing ) after argument list
I have placed an end bracket everywhere in the line of code and it still doesn't work. Can someone please explain the error and tell me what to do to fix it?
A function definition needs to have parentheses for its arguments, even if there are none.
function(){}

Why does this code not function correctly? [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 6 years ago.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Improve this question
I'm currently trying to learn Ajax and have been following a tutorial. I have created the following script:
<!DOCTYPE html>
<html>
<head>
<script type="text/javascript">
function MyFunction(){
var xmlhttp;
if(window.XMLHttpRequest){
xmlhttp = new XMLHttpRequest();
}
else {
xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange = function(){
if(ajaxRequest.readyState == 4){
document.getElementById("MyDiv").innerHTML = xmlhttp.responseText;
}
}
xmlhttp.open("POST", "command.php", true);
xmlhttp.send();
}
</script>
</head>
<body>
<button onclick="MyFunction()">Execute</button>
<div id="MyDiv"></div>
</body>
</html>
I then have a php file named command.php which just echos the word "success".
Obviously the intended purpose of this script is to return the word "success" when the execute button is clicked. I have followed the tutorial for this very precisely, so I'm unsure of what's wrong with what I've written.
I've also looked at other Ajax tutorials, to see what they might have done differently. It seems that some variants of Ajax look very different to others. I mean by this, a lot of tutorials involve code looking like what I've written, but many others (such as this one) look rather different and involve many '$' signs. Why is this?
Looks like maybe a copy/paste error. I'm guessing:
if(ajaxRequest.readyState == 4){
should be:
if(xmlhttp.readyState == 4){
Also, it doesn't sound like you're using your browsers dev tools. The console should have a message like this in it:
Uncaught ReferenceError: ajaxRequest is not defined
I suggest reading up on the dev tools for your preferred browser.

JS conditions on images [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking us to recommend or find a tool, library or favorite off-site resource are off-topic for Stack Overflow as they tend to attract opinionated answers and spam. Instead, describe the problem and what has been done so far to solve it.
Closed 8 years ago.
Improve this question
I need to develop a sort of calculator which show a set of images and the client can select and depending on the choice he moves to next step of choices. At the end of all the choices he will be given a package depending on the choices of all steps chosen.
A working example of something similar can be found in this link: http://store.virginmedia.com/big-bundles.html and then clicking on the 'help me choose' popup on the left hand side.
Can anyone suggest any methods or libraries I can start from in achieving something like this ?
Okay. So this is pretty basic, but I created a JSFiddle just to get you started. I is not pretty, but it should give you some indication of what is required.
Here is the code, if there is something about it that is unclear, google it:
var selections = {
"opt1": false,
"opt2": false,
"opt3": false,
"opt4": false,
"opt5": false,
"opt6": false
};
$(document).ready(function() {
$('.option').click(function(event) {
var id = event.target.id;
if (selections[id]) {
$('#' + id).removeClass('checked-option');
selections[id] = false;
} else {
$('#' + id).addClass('checked-option');
selections[id] = true;
}
});
$('#btn1').click(function() {
$('#grp1').hide();
$('#grp2').show();
});
$('#btn2').click(function() {
$('#grp2').hide();
$('#grp1').show();
});
$('#btn3').click(function() {
var content = 'Selected:<ul>';
for (var i in selections) {
if (selections[i]) {
content += '<li>' + i + '</li>';
}
}
content += '</ul>';
$('#grp2').hide();
$('#done').html(content);
$('#grp3').show();
});
$('#btn4').click(function() {
$('#grp3').hide();
$('#grp2').show();
});
});

Categories

Resources